我查到的资料说是re.match是从头开始匹配,如果开头没有,就匹配不上
\Aimooc表示以imooc开头的字符串,你后面匹配的是Aimooc开头的,所以匹配不到。这里\A表示的是以指定字符串开头,老师讲义里有讲
小括号代表使用了分组,正则语法就是这样,不必纠结.就像python遇到xxx()就知道是调用函数一样.
\1 等于 前面括号里的匹配规则,就相当于 ([\w]+>)\1 = ([\w]+>)[\w]+>两次这个匹配规则.你可以理解成\1是个变量,保存了前面括号里的匹配语法并使用 即 \1 = (匹配语法), 使用\1 就是使用括号里语法 有帮助的话请给个最佳回答
问题都描述不清楚? 什么问题?
我试了下可以啊(python3)
import urllib3 http = urllib3.PoolManager() html = http.request('get', 'https://www.imooc.com').data.decode() print(html)
\1表示使用编号为1的分组,在前面有个括号([\w]+>) 这个括号内的就表示是编号为1的分组,如果这个正则表达式中有多个括号,就是说有多个分组,然后想复用第n个分组,就加一个\n,就OK了,不知道有没有说清楚
问得好?
比如匹配ioo,^ioo匹配的必须是以i开头的,用\A必须是匹配以ioo开头的
因为后边的字符串'<book>'不满足前面的前面的正则要求,重点是弄明白\1是什么意思。可以做两个测试:
测试1:pa = re.match(r'<([\w]+>)\1', '<book>book>')
pa.groups()
测试2:pa = re.match(r'<([\w]+>)', '<book>')
pa.groups()
[\w]{4,10}@163.com$
我想是先匹配后边‘@163.com’,然后找前边的字符4到10位
\1就是代表了前面“([\w]+>)”这些内容,你将\1替换掉就成了ma=re.match(r'<([\w]+>)[\w]+</([\w]+>)','<book>python</book>') 其中括号已经没有意义,去掉后就变成ma=re.match(r'<[\w]+>[\w]+</[\w]+>','<book>python</book>') 这样看就应该没问题了吧。
后面加1匹配不出来的原因也是应为</book>这个字符串匹配不到的缘故。
你仔细看清楚他第一个分组是(book>)已经包含了>
大爷的,没仔细看。
match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.
意思是必须以符合规则的为开头
试了一下,发现 [1-9]? 这个东西发生是看情况的,需要他发生零次时他就发生0次,需要他发生一次时他就发生一次。
老师的例子里,这句话没发生作用,
我刚看的时候也有这个问题,我是这样理解的,因为表达式中的$决定了它前面的数字(即0)该是被匹配串的最后一位,与事实相悖,所以匹配不到
可以这么用,看个人习惯,前者可读性稍微好一些
\1代表第一个分组所引用的内容,改为要下语句:ma=re.match(r'<([\w]+>)\1','<book>book>')
多行问题
首先,^放在[...]里边才会表示反义,例如[^a]表示匹配除了a以外的字符,如果^放在[]外面,则表示以[...]字符为开头
还有在[...]中不需要使用“|”表示或的关系,[...]中的“|”被认为需要匹配“|”这个字符
Hello,我试了一下,其实是这样的:
正则表达式是从前往后一个一个字符匹配,如果走完了你的正则表达式,没出现问题,就返回匹配值。
比如r'[abc]',它匹配abc字符中的一个,'ab'中匹配完a之后,正则表达式运行完毕,没有问题,返回了a,结束。
但是在r'{[abc]}'中,先匹配了{,然后匹配abc中的一个,在你的例子里是a,然后匹配}却匹配不到,因为你的字符串里这时是'b}',它匹配},找到了b,认为有问题,直接匹配就不成功了。
不不妨尝试ma=re.match(r'{[abc]','{ab}'),这样能返回匹配结果'{a',即一个{加上abc中的一个字母。
希望我解释清楚了,如果不明白可以问我。
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 first Java
And
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.
匹配helloworld或者helloworldworld ? 是这个意思吗?
import re ma = re.match(r'(hello)(world)\2?', 'helloworld') print ma.group() ma = re.match(r'(hello)(world)\2?', 'helloworldworld') print ma.group()
把?放在后面
url是什么?
不是xmi是xml,就是类似于html的标签语言。
number分组是引用前面的规则,即重复的部分用分组号代替就行,和后面的别名分组类似
ma = re.match(r'<([\w]+>)\1','<book>book>') print ma.groups() print ma.group()
\1 --> ([\w]+>) --> book>
你是K大写啊, 当然不匹配,所以是NoneType啊
大概是因为re.match方法是从字符串开头匹配,如果用re.search可以匹配到9。