如何在python中使用带有可选字符的regex ?

时间:2021-01-21 21:46:21

Say I have a string

假设我有一根绳子

"3434.35353"

and another string

和另一个字符串

"3593"

How do I make a single regular expression that is able to match both without me having to set the pattern to something else if the other fails? I know \d+ would match the 3593, but it would not do anything for the 3434.35353, but (\d+\.\d+) would only match the one with the decimal and return no matches found for the 3593.

如何创建一个既能匹配又能匹配的正则表达式呢?我知道\d+会匹配3593,但是它不会对3434.35353做任何事情,但是(\d+\ d+)只会匹配小数,并且不会返回3593的匹配。

I expect m.group(1) to return:

我期望m.a.(1)返回:

"3434.35353"

or

"3593"

4 个解决方案

#1


34  

You can put a ? after a group of characters to make it optional.

你可以放一个?在一组字符后使其可选。

You want a dot followed by any number of digits \.\d+, grouped together (\.\d+), optionally (\.\d+)?. Stick that in your pattern:

你想要一个点后面跟着任何数字\。\ d +,组合在一起(\ \ d +),可选(\ \ d +)?。按照你的模式:

import re
print re.match("(\d+(\.\d+)?)", "3434.35353").group(1)
3434.35353
print re.match("(\d+(\.\d+)?)", "3434").group(1)
3434

#2


3  

This regex should work:

这个正则表达式应该工作:

\d+(\.\d+)?

It matches one ore more digits (\d+) optionally followed by a dot and one or more digits ((\.\d+)?).

它可选地匹配一个或多个数字(\d+),后跟一个点和一个或多个数字(\.\d+)?

#3


1  

Use the "one or zero" quantifier, ?. Your regex becomes: (\d+(\.\d+)?).

使用“1或0”量词?你的正则表达式变得:(\ d +(\ \ d +)?)。

See Chapter 8 of the TextWrangler manual for more details about the different quantifiers available, and how to use them.

有关可用的不同量词以及如何使用它们的详细信息,请参阅TextWrangler手册的第8章。

#4


1  

Read up on the Python RegEx library. The link answers your question and explains why.

阅读Python RegEx库。这个链接回答了你的问题并解释了原因。

However, to match a digit followed by more digits with an optional decimal, you can use

但是,要匹配一个数字,后面跟着更多的数字,并使用一个可选的小数,您可以使用

re.compile("(\d+(\.\d+)?)")

In this example, the ? after the .\d+ capture group specifies that this portion is optional.

在这个例子中,?在。\d+ capture组之后,指定该部分是可选的。

Example

例子

#1


34  

You can put a ? after a group of characters to make it optional.

你可以放一个?在一组字符后使其可选。

You want a dot followed by any number of digits \.\d+, grouped together (\.\d+), optionally (\.\d+)?. Stick that in your pattern:

你想要一个点后面跟着任何数字\。\ d +,组合在一起(\ \ d +),可选(\ \ d +)?。按照你的模式:

import re
print re.match("(\d+(\.\d+)?)", "3434.35353").group(1)
3434.35353
print re.match("(\d+(\.\d+)?)", "3434").group(1)
3434

#2


3  

This regex should work:

这个正则表达式应该工作:

\d+(\.\d+)?

It matches one ore more digits (\d+) optionally followed by a dot and one or more digits ((\.\d+)?).

它可选地匹配一个或多个数字(\d+),后跟一个点和一个或多个数字(\.\d+)?

#3


1  

Use the "one or zero" quantifier, ?. Your regex becomes: (\d+(\.\d+)?).

使用“1或0”量词?你的正则表达式变得:(\ d +(\ \ d +)?)。

See Chapter 8 of the TextWrangler manual for more details about the different quantifiers available, and how to use them.

有关可用的不同量词以及如何使用它们的详细信息,请参阅TextWrangler手册的第8章。

#4


1  

Read up on the Python RegEx library. The link answers your question and explains why.

阅读Python RegEx库。这个链接回答了你的问题并解释了原因。

However, to match a digit followed by more digits with an optional decimal, you can use

但是,要匹配一个数字,后面跟着更多的数字,并使用一个可选的小数,您可以使用

re.compile("(\d+(\.\d+)?)")

In this example, the ? after the .\d+ capture group specifies that this portion is optional.

在这个例子中,?在。\d+ capture组之后,指定该部分是可选的。

Example

例子