想要在 Python 中使用正则表达式以字符 类 和重复量词对字符进行分组的方式对单词进行分组
Want to use regular expression in Python to group words the way character classes and repetition quantifiers group characters
我有一个问题想在 python 中使用 re.如果我需要扩展功能,我可以使用正则表达式,但我的需求似乎很简单。
例如我有以下文字:
one
one two
one two three
one two three four
one two three four five
我想取回带有 1、2 或 3 个单词的行,绕过其余部分,因此在上面的示例文本中,前 3 行将匹配,其他行将不匹配。
文本是由 tesseract ocr 实用程序生成的,所以文本可以是任何东西,乱码错误等等,所以我在字符 class 中使用 \S 查找单词,如下所示:[\S]+
我有以下有效的正则表达式:
^[\S]+[ ]?[\S]+?[ ]?[\S]+?[ ]?$
问题是,我正处于开发的原型阶段,我猜我需要扩展它以接受最多六个单词,同时只跳过一个单词的行。
可以很容易地扩展正则表达式来执行此操作,但我几乎可以肯定我最终将需要一个正则表达式来连续捕获最多三行,以满足我的单行标准。
所以,感谢 regular-expressions.info,我正在学习更多关于正则表达式的知识,并了解字符 classes -- [] 和重复量词 -- {} 是如何工作的。
我想要的是一种使用字符 classes 和重复量词而不是字符对单词进行分组的方法。
我知道我可以在 python 中逐行完成这一切,但是使用正则表达式可以让我扩展我的解决方案,因为我在构建的工具中遇到更多的 ocr 输出。
关于如何进行的任何帮助?
---- 添加测试代码和 pythex.org 详细说明不同行为的屏幕截图。
Wiktor 的正则表达式是 ^\S+(?: \S+){0,2}$
测试代码:
import re
def testre(pattern, text):
p = re.compile(pattern, re.M)
results = p.findall(text)
print(f'Test Results: {results}')
txt = 'one\none two\none two three\none two three four\none two three four five\n'
pattern1 = r'^\S+(?: \S+){0,2}$'
print(f'Test string...\n{txt}')
print(f'Test regex: {pattern1}')
testre(pattern1, txt)
运行时显示 Wiktor 的表达式按预期工作:
Test string...
one
one two
one two three
one two three four
one two three four five
Test regex: ^\S+(?: \S+){0,2}$
Test Results: ['one', 'one two', 'one two three']
然而,在 pythex.org 上的 运行 显示 'one two' 不匹配:
Wiktor 的正则表达式有效,但在 pythex.org 上看到的不同结果有点令人担忧。我希望使用 pythex 进行测试。
关于 ^[\S]+[ ]?[\S]+?[ ]?[\S]+?[ ]?$
模式,您应该了解几件事:1) 它不会匹配 1 个或 2 个字符的“单词”,因为 \S+
、[\S]+?
和 [\S]+?
在这里每个都需要至少一个非空白字符,2) 你不应该(over|ab)使用字符 类,[\S]
= \S
,但是 [\b]
!= \b
和 [.]
!= .
。只在必要时使用字符 类 以避免弄乱模式。
你可以使用
^\S+(?: \S+){0,2}$ # One to three "word" string with a regular space between the words only
^\S+(?:\s\S+){0,2}$ # One to three "word" string with any whitespace between the words only
^\S+(?:\s+\S+){0,2}$ # One to three "word" string with one or more whitespace chars between the words only
^\s*\S+(?:\s+\S+){0,2}\s*$ # One to three "word" string with one or more whitespace chars between
# the words and allowing leading/trailing whitespace in the string
注意:\S+
匹配任何 1+ 个非空白字符,它可以匹配 abc
、abc123,
、---++===
,等等。如果你想匹配单词(字母,数字,_
),你可以使用\w+
。如果只想匹配字母单词,请使用 [^\W\d_]+
.
详情
^
- 字符串开头
\s*
- 零个或多个空白字符
\S+
- 一个或多个非空白字符
(?:\s+\S+){0,2}
- 一个或多个任意空白字符出现零到两次,然后是一个或多个非空白字符
\s*
- 零个或多个空白字符
$
- 字符串结尾。
我有一个问题想在 python 中使用 re.如果我需要扩展功能,我可以使用正则表达式,但我的需求似乎很简单。
例如我有以下文字:
one
one two
one two three
one two three four
one two three four five
我想取回带有 1、2 或 3 个单词的行,绕过其余部分,因此在上面的示例文本中,前 3 行将匹配,其他行将不匹配。
文本是由 tesseract ocr 实用程序生成的,所以文本可以是任何东西,乱码错误等等,所以我在字符 class 中使用 \S 查找单词,如下所示:[\S]+
我有以下有效的正则表达式:
^[\S]+[ ]?[\S]+?[ ]?[\S]+?[ ]?$
问题是,我正处于开发的原型阶段,我猜我需要扩展它以接受最多六个单词,同时只跳过一个单词的行。
可以很容易地扩展正则表达式来执行此操作,但我几乎可以肯定我最终将需要一个正则表达式来连续捕获最多三行,以满足我的单行标准。
所以,感谢 regular-expressions.info,我正在学习更多关于正则表达式的知识,并了解字符 classes -- [] 和重复量词 -- {} 是如何工作的。
我想要的是一种使用字符 classes 和重复量词而不是字符对单词进行分组的方法。
我知道我可以在 python 中逐行完成这一切,但是使用正则表达式可以让我扩展我的解决方案,因为我在构建的工具中遇到更多的 ocr 输出。
关于如何进行的任何帮助?
---- 添加测试代码和 pythex.org 详细说明不同行为的屏幕截图。
Wiktor 的正则表达式是 ^\S+(?: \S+){0,2}$
测试代码:
import re
def testre(pattern, text):
p = re.compile(pattern, re.M)
results = p.findall(text)
print(f'Test Results: {results}')
txt = 'one\none two\none two three\none two three four\none two three four five\n'
pattern1 = r'^\S+(?: \S+){0,2}$'
print(f'Test string...\n{txt}')
print(f'Test regex: {pattern1}')
testre(pattern1, txt)
运行时显示 Wiktor 的表达式按预期工作:
Test string...
one
one two
one two three
one two three four
one two three four five
Test regex: ^\S+(?: \S+){0,2}$
Test Results: ['one', 'one two', 'one two three']
然而,在 pythex.org 上的 运行 显示 'one two' 不匹配:
Wiktor 的正则表达式有效,但在 pythex.org 上看到的不同结果有点令人担忧。我希望使用 pythex 进行测试。
关于 ^[\S]+[ ]?[\S]+?[ ]?[\S]+?[ ]?$
模式,您应该了解几件事:1) 它不会匹配 1 个或 2 个字符的“单词”,因为 \S+
、[\S]+?
和 [\S]+?
在这里每个都需要至少一个非空白字符,2) 你不应该(over|ab)使用字符 类,[\S]
= \S
,但是 [\b]
!= \b
和 [.]
!= .
。只在必要时使用字符 类 以避免弄乱模式。
你可以使用
^\S+(?: \S+){0,2}$ # One to three "word" string with a regular space between the words only
^\S+(?:\s\S+){0,2}$ # One to three "word" string with any whitespace between the words only
^\S+(?:\s+\S+){0,2}$ # One to three "word" string with one or more whitespace chars between the words only
^\s*\S+(?:\s+\S+){0,2}\s*$ # One to three "word" string with one or more whitespace chars between
# the words and allowing leading/trailing whitespace in the string
注意:\S+
匹配任何 1+ 个非空白字符,它可以匹配 abc
、abc123,
、---++===
,等等。如果你想匹配单词(字母,数字,_
),你可以使用\w+
。如果只想匹配字母单词,请使用 [^\W\d_]+
.
详情
^
- 字符串开头\s*
- 零个或多个空白字符\S+
- 一个或多个非空白字符(?:\s+\S+){0,2}
- 一个或多个任意空白字符出现零到两次,然后是一个或多个非空白字符\s*
- 零个或多个空白字符$
- 字符串结尾。