本来只想匹配 . ,但是在这里这个点代表了所有字符,于是返回了1 2 3。这个. 就是一个转义表达式。
分别提取含7-9的数字或3-4的数字的字符串
^XX就是以XX开头的意思
当^放在方括号内是取非的意思
2{2,3}是寻找2倍重复了2到3次的意思
2{2,}是寻找2倍重复了大于2次(2到+无穷)的意思
fo+表示+紧跟着的o可以被重复1次或1次以上
大括号也可以起到这样的作用
若想让紧跟在o后面的对fo都起作用,需要小括号
与+用法一致。+表示重复了1次或1次以上,*表示0次或0次以上。
.* 可以匹配任意字符
管道符连接前后,符合其一即会被返回
\\b也可以起到一样的作用,但\\b更灵活,既可以放开头也可以放结尾。(\\b主要是匹配边界)
\的作用就是说明^不是开头的意思,是要去匹配这个符号
对所有的转义符号都适用
一般情况下可以这样匹配中文,如图:&ampltimg src="https://pic4.zhimg.com/50/edcbd2faf1a916675cec852bd886e599_hd.jpg" data-rawwidth="827" data-rawheight="600" class="origin_image zh-lightbox-thumb" width="827" data-original="https://pic4.zhimg.com/edcbd2faf1a916675cec852bd886e599_r.jpg"&ampgt
先用靓汤或正则找到这个节点,再用上面的字符组匹配。
假设这个节点只有一个,用法如下:
import reimport requests as reqfrom bs4 import BeautifulSoupurl = 'xxx'html = req.get(url).textbs = BeautifulSoup(html)span = bs.find_all('span', 'pro-title')'''span = re.findall('<span\sclass="pro-title">[^<]+</span>', html)s = span[0]m = re.findall('[\u4e00-\u9fa5]+', s)'''s = str(span)m = re.findall('[\u4e00-\u9fa5]+', s)print(m)
public void ShowStructure()
{
//要匹配的字符串
string text = "早上好aaa您好bbb大家好ddd……"
//正则表达式
string pattern = @"[\u4e00-\u9fff]+"
Regex r = new Regex(pattern)
//使用正则表达式匹配字符串,仅返回一次匹配结果
Match m = r.Match(text)
while (m.Success)
{
//显示匹配开始处的索引值和匹配到的值
System.Console.WriteLine("Match=[" + m + "]")
CaptureCollection cc = m.Captures
foreach (Capture c in cc)
{
Console.WriteLine("\tCapture=[" + c + "]")
}
for (int i = 0 i < m.Groups.Count i++)
{
Group group = m.Groups[i]
System.Console.WriteLine("\t\tGroups[{0}]=[{1}]", i, group)
for (int j = 0 j < group.Captures.Count j++)
{
Capture capture = group.Captures[j]
Console.WriteLine("\t\t\tCaptures[{0}]=[{1}]", j, capture)
}
}
//进行下一次匹配.
m = m.NextMatch()
}
}