Python美元正则匹配
时间: 2023-11-05 17:00:41 浏览: 38
美元符号在正则表达式中表示匹配字符串结尾的位置。在Python中,可以使用 `$` 符号来表示美元符号。
例如,如果要匹配以 "hello" 结尾的字符串,可以使用以下正则表达式:
```python
import re
pattern = r"hello$"
text = "Say hello to the world"
match = re.search(pattern, text)
if match:
print("Match found:", match.group())
else:
print("Match not found")
```
输出结果为:
```
Match found: hello
```
在这个例子中,正则表达式 `hello$` 匹配以 "hello" 结尾的字符串,因此与文本字符串 "Say hello to the world" 匹配成功。
相关问题
python正则表达式的写法
printf("%c ", 'A' + i);
}
printf("\n");
// 判断胜负
winner = check_win(player == BLACK ? WHITE : BLACK);
if (winner != 0) {
if (winner == BLACK) {
printf("BlackPython中正则表达式的写法需要用到re模块,以下是一些常见的正则表达式 wins!\n");
} else {
printf("White wins!\n");
}
break;
}
// 落子
写法:
1. 匹配任意字符:用点号(.)表示,例如 a.b 可以匹配 "acb" printf("%s's turn (enter 'q' to quit): ", player == BLACK ? "Black" : "White");
while (、"a1b" 等字符串。
2. 匹配某个字符集合:用方括号([])表示,例如1) {
scanf("%d %d", &x, &y);
if (x == -1 && y == -1) [abc] 匹配 a、b 或 c 中的任意一个字符。
3. 匹配多个字符:用星号( { // 退出游戏
printf("Game over.\n");
return 0;
}
if (x ==*)表示,例如 a* 可以匹配 "a"、"aa"、"aaa" 等任意数量的字符。
-2 && y == -2) { // 悔棋
printf("Not implemented.\n");
continue;
}
4. 匹配一个或多个字符:用加号(+)表示,例如 a+ 可以匹配 "a"、 if (x == -3 && y == -3) { // 认输
printf("%s resigns. %s wins!\"aa"、"aaa" 等至少一个字符的字符串。
5. 匹配零个或一个字符:用问号(n", player == BLACK ? "Black" : "White", player == BLACK ? "White" : "Black");
return 0;
?)表示,例如 a? 可以匹配空字符串、"a" 等一个字符的字符串。
6. 匹配重复 }
if (x >= 1 && x <= BOARD_SIZE && y >= 1 && y <= BOARD_SIZE && is_empty(x -次数:用花括号({})表示,例如 a{3,5} 可以匹配 "aaa"、"aaaa 1, y - 1)) {
if (check_forbidden(x - 1, y - 1, player)) { //"、"aaaaa" 等重复出现 3-5 次的字符串。
7. 匹配开头或结尾: 禁手
printf("Forbidden move.\n");
continue;
}
place(x - 1, y - 1,用脱字符号(^)表示字符串开头,用美元符号($)表示字符串结尾,例如 ^a 表 player);
break;
}
printf("Invalid move.\n");
}
// 切换玩家
player = player示以 "a" 开头的字符串,a$ 表示以 "a" 结尾的字符串。
8. 匹配指定 == BLACK ? WHITE : BLACK;
}
return 0;
}
```
注意:该代码只是一个简单的围棋字符:用反斜杠(\)表示,例如 \d 表示任意数字字符,\s 表示任意空白游戏,还有很多功能没有实现(如悔棋、保存游戏等),且禁手规则可能不完全准确,仅供参考。
对上面的代码生成正则表达式匹配Steven Adams
可以使用如下正则表达式匹配该 HTML 代码中的 Steven Adams:
```python
import re
html_code = '<tr> <td class="td_padding"> <a href="https://nba.hupu.com/players/stevenadams-4829.html" target="_blank"> <img src="https://gdc.hupucdn.com/gdc/nba/players/uploads/gamespace/players/34c826b1f90841f64d0347ff94514e71.png"/> </a> </td> <td class="left"> <b><a href="https://nba.hupu.com/players/stevenadams-4829.html" target="_blank">史蒂文-亚当斯</a></b> <p>(<b>Steven Adams</b>)</p> </td> <td>4</td> <td>C</td> <td>2.11米/6尺11</td> <td>120公斤/265磅</td> <td>1993-07-20</td> <td class="left">4年1亿美元,2017年夏天生效,2021年到期。2020年以2年3500万美元提前续约,2021年生效。<br/><b>本年薪金:2753万美元</b></td> </tr>'
match = re.search(r'<b><a.*?>(.*?)</a></b>', html_code)
if match:
print(match.group(1))
else:
print("No match")
```
该正则表达式为 `<b><a.*?>(.*?)</a></b>`,使用 `re.search()` 函数搜索匹配,最终输出 "史蒂文-亚当斯"。