regex 正则

resource

  • regex101: build, test, and debug regular expressions.

?<=?=

?<= : 匹配以字符串开始,捕获(存储)

?=: 匹配以字符串结束,捕获(存储)

正则表达式:

  • (?<=(href=")).{1,200}(?=(">)): 捕获以href="开头的字符串最长达到 200 个字符且以">结尾的字符串

解释:

  • (?<=(href=")): 匹配以href="开头的字符串,并且捕获(存储)到分组中

  • (?=(">)) : 匹配以">结尾的字符串,并且捕获(存储)到分组中

示例如下:

V8kWG2NQwsSoPX4.

?<=(?:) : 匹配以字符串开始, 捕获(存储)

?=(?:): 匹配以字符串结束, 捕获(存储)

正则表达式:

  • (?<=(?:href=")).{1,200}(?=(?:">))

解释:

  • (?<=(?:href=")): 匹配以href="开头的字符串,并且不捕获(不存储)到分组中

  • (?=(?:">)): 匹配以">结尾的字符串,并且不捕获(不存储)到分组中

参考教程:

^$

在正则表达式中,^表示字符串的开头,$表示字符串的结尾。

例如,要匹配以hello开头的字符串,可以使用正则表达式^hello。要匹配以world结尾的字符串,可以使用正则表达式world$

在使用正则表达式时,还需要注意转义字符的使用。例如,如果要匹配以.开头的字符串,可以使用正则表达式\.,其中\是转义字符,用于将.转义为普通字符。

下面是一些示例正则表达式,用于匹配字符串的开头和结尾:

  • ^hello:匹配以hello开头的字符串。

  • world$:匹配以world结尾的字符串。

  • ^\d+:匹配以数字开头的字符串。

  • ^[a-zA-Z]+:匹配以字母开头的字符串。

  • \.txt$:匹配以.txt结尾的字符串。

(?<name>pattern)

命名捕获分组用于将捕获的内容存储到一个变量中,具体访问遵循特定的语言标准。

  • (?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2}): 将捕获的内容存储到变量yearmonthday

在Python中,可以使用 groupdict() 方法来获取一个字典,其中键是捕获组的名称,值是捕获组的值。

1
2
3
4
5
6
7
8
9
10
import re

pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
match = re.match(pattern, '2022-01-01')

if match:
groups = match.groupdict()
print(groups['year']) # 输出: 2022
print(groups['month']) # 输出: 01
print(groups['day']) # 输出: 01

在C中,标准库的正则表达式库<regex>不直接支持命名捕获组的语法。但可以使用第三方库,如Boost.Regex,来实现命名捕获组的功能。以下是使用Boost.Regex在C中实现命名捕获组的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <boost/regex.hpp>

int main() {
std::string input = "2022-01-01";
boost::regex pattern("(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})");
boost::smatch match;

if (boost::regex_match(input, match, pattern)) {
std::cout << match["year"] << std::endl; // 输出: 2022
std::cout << match["month"] << std::endl; // 输出: 01
std::cout << match["day"] << std::endl; // 输出: 01
}

return 0;
}