非连字符的正则表达式匹配 - Python
Regex Match for Non Hyphenated Words - Python
我正在尝试在 Python 中为非连字词创建一个正则表达式,但我无法找出正确的语法。
正则表达式的要求是:
- 它不应包含连字符 AND
- 它应该至少包含 1 个数字
我试过的表达方式是:=
^(?!.*-)
- 这匹配所有非连字符的单词,但我不知道如何另外添加第二个条件。
^(?!.*-(?=/d{1,}))
- 我尝试使用双重先行,但我不确定使用它的语法。这匹配 ID101 但也匹配 Whosebug
应该匹配的示例词:
1DRIVE , ID100 , W1RELESS
不应该匹配的示例词:
基本上任何非数字字符串(如 STACK 、 OVERFLOW )或任何带连字符的单词( Test-11 、 24-hours)
附加信息:
我正在使用库 re 并编译正则表达式模式并使用 re.search 进行匹配。
任何帮助都将非常有帮助,因为我是正则表达式匹配的新手,并且坚持了好几个小时。
也许,
(?!.*-)(?=.*\d)^.+$
可能只是工作正常。
测试
import re
string = '''
abc
abc1-
abc1
abc-abc1
'''
expression = r'(?m)(?!.*-)(?=.*\d)^.+$'
print(re.findall(expression, string))
输出
['abc1']
如果您希望 simplify/modify/explore 表达式,regex101.com. If you'd like, you can also watch in this link 的右上面板已对其进行说明,它将如何匹配一些示例输入。
正则表达式电路
jex.im 可视化正则表达式:
正则表达式 101 解释
/
(?!.*-)(?=.*\d)^.+$
/
gm
Negative Lookahead (?!.*-)
Assert that the Regex below does not match
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
- matches the character - literally (case sensitive)
Positive Lookahead (?=.*\d)
Assert that the Regex below matches
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equal to [0-9])
^ asserts position at start of a line
.+ matches any character (except for line terminators)
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
我想到了 -
^[^-]*\d[^-]*$
- 所以我们至少需要一位数字 (
\d
)
- 我们需要字符串的其余部分包含除 - (
[^-]
) 之外的任何内容
- 我们可以有无限数量的这些字符,所以
[^-]*
- 但是像
[^-]*\d
那样将它们放在一起会在 aaa3-
上失败,因为 - 出现在有效匹配之后 - 让我们确保在我们的匹配之前或之后没有破折号可以潜入 ^[-]*\d$
不幸的是,这意味着 aaa555D
失败了。所以我们实际上需要再次添加第一组 - ^[^-]*\d[^-]$
--- 开始 - 任意数量的非破折号字符 - 数字 - 任意数量的非破折号字符 - end
根据样式,我们也可以做 ^([^-]*\d)+$
因为 digits/numbers 的顺序无关紧要,我们可以有任意多个。
然而,最后...这就是我实际解决这个特定问题的方式,因为正则表达式可能很强大,但它们往往会使代码更难理解...
if ("-" not in text) and re.search("\d", text):
我正在尝试在 Python 中为非连字词创建一个正则表达式,但我无法找出正确的语法。
正则表达式的要求是:
- 它不应包含连字符 AND
- 它应该至少包含 1 个数字
我试过的表达方式是:=
^(?!.*-)
- 这匹配所有非连字符的单词,但我不知道如何另外添加第二个条件。
^(?!.*-(?=/d{1,}))
- 我尝试使用双重先行,但我不确定使用它的语法。这匹配 ID101 但也匹配 Whosebug
应该匹配的示例词: 1DRIVE , ID100 , W1RELESS
不应该匹配的示例词: 基本上任何非数字字符串(如 STACK 、 OVERFLOW )或任何带连字符的单词( Test-11 、 24-hours)
附加信息:
我正在使用库 re 并编译正则表达式模式并使用 re.search 进行匹配。
任何帮助都将非常有帮助,因为我是正则表达式匹配的新手,并且坚持了好几个小时。
也许,
(?!.*-)(?=.*\d)^.+$
可能只是工作正常。
测试
import re
string = '''
abc
abc1-
abc1
abc-abc1
'''
expression = r'(?m)(?!.*-)(?=.*\d)^.+$'
print(re.findall(expression, string))
输出
['abc1']
如果您希望 simplify/modify/explore 表达式,regex101.com. If you'd like, you can also watch in this link 的右上面板已对其进行说明,它将如何匹配一些示例输入。
正则表达式电路
jex.im 可视化正则表达式:
正则表达式 101 解释
/
(?!.*-)(?=.*\d)^.+$
/
gm
Negative Lookahead (?!.*-)
Assert that the Regex below does not match
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
- matches the character - literally (case sensitive)
Positive Lookahead (?=.*\d)
Assert that the Regex below matches
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equal to [0-9])
^ asserts position at start of a line
.+ matches any character (except for line terminators)
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
我想到了 -
^[^-]*\d[^-]*$
- 所以我们至少需要一位数字 (
\d
) - 我们需要字符串的其余部分包含除 - (
[^-]
) 之外的任何内容
- 我们可以有无限数量的这些字符,所以
[^-]*
- 但是像
[^-]*\d
那样将它们放在一起会在aaa3-
上失败,因为 - 出现在有效匹配之后 - 让我们确保在我们的匹配之前或之后没有破折号可以潜入^[-]*\d$
不幸的是,这意味着
aaa555D
失败了。所以我们实际上需要再次添加第一组 -^[^-]*\d[^-]$
--- 开始 - 任意数量的非破折号字符 - 数字 - 任意数量的非破折号字符 - end根据样式,我们也可以做
^([^-]*\d)+$
因为 digits/numbers 的顺序无关紧要,我们可以有任意多个。
然而,最后...这就是我实际解决这个特定问题的方式,因为正则表达式可能很强大,但它们往往会使代码更难理解...
if ("-" not in text) and re.search("\d", text):