課程
/后端開發(fā)
/Python
/python正則表達(dá)式
^和\A的區(qū)別
$和\Z的區(qū)別
2016-10-24
源自:python正則表達(dá)式 3-3
正在回答
public?static?void?main(String[]?args)?{? ????????String?matcherStr?=?"This?is?the?first?Java"; ????????matcherStr?+=?"\nAnd"; ????????matcherStr?+=?"\nThis?is?the?second?Python"; ????????Matcher?matcher?=?Pattern.compile("Java$",?Pattern.MULTILINE).matcher(matcherStr); ???????? ????????int?i=0; ????????while(matcher.find()){ ???????????i++; ????????} ????????System.out.println(i); }
There is one match (i=1) for Java in the first line. This is mutiline, so the whole matcherStr is something like:
This is the first Java
And
This is the second Python
public?static?void?main(String[]?args)?{? ????????String?matcherStr?=?"This?is?the?first?Java"; ????????matcherStr?+=?"\nAnd"; ????????matcherStr?+=?"\nThis?is?the?second?Java"; ????????Matcher?matcher?=?Pattern.compile("Java$",?Pattern.MULTILINE).matcher(matcherStr); ???????? ????????int?i=0; ????????while(matcher.find()){ ???????????i++; ????????} ????????System.out.println(i); }
There are two matchs (i=2) for Java in the first line. This is mutiline, so the whole matcherStr is something like:
This is the second Java
public?static?void?main(String[]?args)?{? ????????String?matcherStr?=?"This?is?the?first?Java"; ????????matcherStr?+=?"\nAnd"; ????????matcherStr?+=?"\nThis?is?the?second?Python"; ????????Matcher?matcher?=?Pattern.compile("Java$").matcher(matcherStr); ???????? ????????int?i=0; ????????while(matcher.find()){ ???????????i++; ????????} ????????System.out.println(i); }
There is no match?(i=0) for Java. This is not mutiline, so the whole matcherStr is something like:
This is the first Java\nAnd\nThis is the second Python
public?static?void?main(String[]?args)?{? ????????String?matcherStr?=?"This?is?the?first?Java"; ????????matcherStr?+=?"\nAnd"; ????????matcherStr?+=?"\nThis?is?the?second?Java"; ????????Matcher?matcher?=?Pattern.compile("Java\\Z",?Pattern.MULTILINE).matcher(matcherStr); ???????? ????????int?i=0; ????????while(matcher.find()){ ???????????i++; ????????} ????????System.out.println(i); }
There is one match?(i=1) for Java with multiline.? The same as without multiline.
In this case: If Pattern.compile("Java$",?Pattern.MULTILINE), there are two matches.
public?static?void?main(String[]?args)?{? ????????String?matcherStr?=?"This?is?the?first?Java"; ????????matcherStr?+=?"\nAnd"; ????????matcherStr?+=?"\nThis?is?the?second?Java"; ????????Matcher?matcher?=?Pattern.compile("Java\\Z").matcher(matcherStr); ???????? ????????int?i=0; ????????while(matcher.find()){ ???????????i++; ????????} ????????System.out.println(i); }
There is one match?(i=1) for Java without multiline.
cc在哪 提問者
^?指定匹配必須出現(xiàn)在字符串的開頭或行的開頭。
\A?指定匹配必須出現(xiàn)在字符串的開頭(忽略?Multiline?選項(xiàng))。
$?指定匹配必須出現(xiàn)在以下位置:字符串結(jié)尾、字符串結(jié)尾的?\n?之前或行的結(jié)尾。 \Z?指定匹配必須出現(xiàn)在字符串的結(jié)尾或字符串結(jié)尾的?\n?之前(忽略?Multiline?選項(xiàng))。?
慕粉3863858
慕粉3936973 回復(fù) cc在哪 提問者
慕粉3936973 回復(fù) 慕粉3863858
舉報(bào)
如何使用正則處理文本,帶你對python正則有個(gè)全面了解
1 回答正則表達(dá)式$與\Z的區(qū)別
2 回答正則表達(dá)式
1 回答正則表達(dá)式
1 回答關(guān)于正則表達(dá)式中的\
1 回答關(guān)于正則表達(dá)式中[]的問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-10-31
There is one match (i=1) for Java in the first line. This is mutiline, so the whole matcherStr is something like:
This is the first Java
And
This is the second Python
There are two matchs (i=2) for Java in the first line. This is mutiline, so the whole matcherStr is something like:
This is the first Java
And
This is the second Java
There is no match?(i=0) for Java. This is not mutiline, so the whole matcherStr is something like:
This is the first Java\nAnd\nThis is the second Python
There is one match?(i=1) for Java with multiline.? The same as without multiline.
In this case: If Pattern.compile("Java$",?Pattern.MULTILINE), there are two matches.
There is one match?(i=1) for Java without multiline.
2016-10-24
^?
指定匹配必須出現(xiàn)在字符串的開頭或行的開頭。
\A?
指定匹配必須出現(xiàn)在字符串的開頭(忽略?Multiline?選項(xiàng))。
$?
指定匹配必須出現(xiàn)在以下位置:字符串結(jié)尾、字符串結(jié)尾的?\n?之前或行的結(jié)尾。
\Z?
指定匹配必須出現(xiàn)在字符串的結(jié)尾或字符串結(jié)尾的?\n?之前(忽略?Multiline?選項(xiàng))。?