使用python regex的Strip特性

2024-04-26 02:43:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的初学者,刚刚学习了regex。
我要做的是使用regex方法制作一个strip特性(strip())。
下面是我写的代码

import regex

stripRegex = regex.compile(r"(\s*)((\S*\s*\S)*)(\s*)")
text = '              Hello World This is me Speaking                                    '
check = stripRegex.search(text)
print(check)
print('group 1 :', stripRegex.search(text).group(1))
print('group 2 :', stripRegex.search(text).group(2))
print('group 3 :', stripRegex.search(text).group(3))
print('group 4 :', stripRegex.search(text).group(4))

结果是

第一组:
第二组:你好,世界,我是
第3组:峰值
第四组:

在这里,我想知道两件事。
1) 为什么第三组返回“峰值”?
2) python是否按顺序识别'('并分配第一个数字?
所以在这段代码中,(\s*)((\s*\s*\s))(\s
第一组(\s*)-是第一组
((\S*\S*\S))-第二个,
(\S
\S*\S)-第三个,
第二个(\s*)-第四个。

我说得对吗?你知道吗


Tags: 方法代码textimportsearchcheckgroup特性
2条回答

你说得对。 \S*\S*\S匹配项:

\S* - at least 0 non-whitespace
\s* - at least 0 whitespace
\S  - one non-whitespace

组3(\S*\S*\S)被重复以馈送组2((\S*\S*\S)*),因此,组3将包含它馈送给组2的最后一个匹配:最后一个可能的匹配是0个或多个非空白,后跟0个或多个空白,后跟一个非空白。这可以用它的第一个匹配来解释:

'Hello T'
\S* matches 'Hello'
\s* matches ' '
\S  matches 'T'

如果你重复这句话,你将从每个单词的前面取第一个字母:

'his i'
\S* matches 'his'
\s* matches ' '
\S  matches 'i'

等等,直到。。。你知道吗

最后一个匹配将省略最后一个单词的第一个字母,不需要任何空格,并且必须以一个非空格结束:

'tring'
\S* matches 'trin'
\s* matches ''      (at least 0 whitespace, so zero)
\S  matches 'g'

问题2:你说得对。从左到右,第一个(是组1的开始,第二个(是组2的开始,以此类推

问题1:组3重复匹配是因为它前面有*。它的最终价值将是最终比赛的价值。组3的匹配项为:

"Hello W" where \S*="Hello"   \s*=" "   \S="W"
"orld T"  where \S*="orld"    \s*=" "   \S="T" 
"his i"   where \S*="his"     \s*=" "   \S="i"
"s m"     where \S*="s"       \s*=" "   \S="m"
"e S"     where \S*="e"       \s*=" "   \S="S"
"peaking" where \S*="peakin"  \s*=""    \S="g"

这里有一个非常好的工具来理解正则表达式:https://regex101.com/r/MmYOPT/1(尽管它对重复匹配没有多大帮助)。你知道吗

相关问题 更多 >