浮点或字符串的正则表达式
我试图找到与浮点或字符串表达式匹配的正则表达式。
即,要匹配的文本可能如下所示:
ABC 3.101
DEF 5.0
HIJ ?Error
KLM 1.0
NOP Range
我当前的版本是:
fp_word = r"(?:[-+]?\d+.\d+|\w+\?)"
但它与 ?Error
或 Range
情况不匹配。
它应该匹配
3.101
5.0
?Error (including the question mark)
1.0
Range
I'm trying to find a regular expression that matches a floating point or a string expression.
I.e. a text to match might look like this:
ABC 3.101
DEF 5.0
HIJ ?Error
KLM 1.0
NOP Range
My current version is:
fp_word = r"(?:[-+]?\d+.\d+|\w+\?)"
but its not matching the ?Error
or Range
case.
It should match
3.101
5.0
?Error (including the question mark)
1.0
Range
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的正则表达式是这样的:
它不匹配非数字字符串,因为您试图匹配 1 个以上单词字符,后跟文字
?
,即字符串后面的?
。而在您的输入中,您只有一个以?
开头的值,而另一个值甚至没有?
,因此两者都无法匹配。如果我正确理解您的要求,您可以使用此正则表达式:
RegEx Demo
它开始与空格并匹配有符号浮点数或 1+ 个非空格,即
\S+
。Your regex is this:
It is not matching non-numeric strings because you are trying to match 1+ word characters followed by a literal
?
i.e.?
after the string. Whereas in your input you have just one value that starts with?
and other one doesn't even have a?
so both are failing to match.If I understand your requirements correctly you can just use this regex:
RegEx Demo
It starts matching with a space and matched either a signed floating point number or 1+ of non-whitespace i.e.
\S+
.您可以使用
查看此正则表达式演示。它匹配除换行符之外的任何一个或多个字符,直到第一个空格之后的行尾。
如果您的正则表达式应该只匹配数字或某些单词(可选地前面带有
?
字符)并且您想使用您的正则表达式,但仅在(非)单词边界匹配,您可以使用请参阅 正则表达式演示。在这里,
(?:\b(?=\w)|\B(?=\W))
- 一个 类型 2 的自适应动态单词边界(YouTube 视频说明):如果下一个字符是单词字符,则匹配单词边界,否则,该位置必须是非-字边界位置(?!^)
- 不是字符串位置的开头(?:[-+]?\d+(?:\.\d+)?|\??\w+) - 任一
[-+]?\d+(?:\.\d+)?
- 可选的+
或-
,然后是一个或更多数字后跟可选的.
序列和一个或多个数字|
- 或\??\w+
- 可选的?
和一个或多个单词字符。You can use
See this regex demo. It matches any one or more chars other than line break chars till the end of a line after the first space.
If your regex should only match a number or some word optionally preceded with a
?
char and you want to use your regex, but only match at a (non)word boundary you can useSee the regex demo. Here,
(?:\b(?=\w)|\B(?=\W))
- an adaptive dynamic word boundary of Type 2 (YouTube video explanation): it matches a word boundary if the next char is a word char, else, the position must be a non-word boundary position(?!^)
- not the start of string position(?:[-+]?\d+(?:\.\d+)?|\??\w+)
- either[-+]?\d+(?:\.\d+)?
- an optional+
or-
and then one or more digits followed with an optional sequence of a.
and one or more digits|
- or\??\w+
- an optional?
and one or more word chars.