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:
import re
print re.match("(\d+(\.\d+)?)", "3434.35353").group(1)
3434.35353
print re.match("(\d+(\.\d+)?)", "3434").group(1)
3434
Related Contents:
- Word boundary with words starting or ending with special characters gives unexpected results
- How to find overlapping matches with a regexp?
- How to use a variable inside a regular expression?
- Do regular expressions from the re module support word boundaries (\b)?
- How to input a regex in string.replace?
- Checking whole string with a regex
- Python non-greedy regexes
- Match text between two strings with regular expression
- Regular expression matching a multiline block of text
- Easiest way to replace a string using a dictionary of replacements?
- Handling backreferences to capturing groups in re.sub replacement pattern
- How can I do multiple substitutions using regex?
- Natural sorting
- Find longest repetitive sequence in a string
- Python Regex instantly replace groups
- Matching Nested Structures With Regular Expressions in Python
- How to remove parentheses and all data within using Pandas/Python?
- How to timeout function in python, timeout less than a second
- BeautifulSoup webscraping find_all( ): finding exact match
- How to extract an IP address from an HTML string?
- Regex: AttributeError: ‘NoneType’ object has no attribute ‘groups’
- RegEx with multiple groups?
- How can a recursive regexp be implemented in python?
- Regex, select closest match
- In Python, how to check if a string only contains certain characters?
- How to use regex to find all overlapping matches
- Recursive pattern in regex
- Split string based on a regular expression
- TypeError: expected string or buffer
- Find the indexes of all regex matches?
- How do I return a string from a regex match in python? [duplicate]
- Passing a function to re.sub in Python
- matching any character including newlines in a Python regex subexpression, not globally
- Find all Chinese text in a string using Python and Regex
- Python regular expressions OR
- How to combine multiple regex into single one in python?
- Split string at every position where an upper-case word starts
- Split string on commas but ignore commas within double-quotes?
- Python/Regex – How to extract date from filename using regular expression?
- “Nothing to repeat” from Python regex
- BeautifulSoup returns empty list when searching by compound class names
- What does the “r” in pythons re.compile(r’ pattern flags’) mean?
- Remove duplicate chars using regex?
- Regular expression to return all characters between two special characters
- Using ^ to match beginning of line in Python regex
- Do Python regular expressions have an equivalent to Ruby’s atomic grouping?
- Python regex split without empty string
- Issue with regex backreference in Python
- Unexpected NoReverseMatch error when using include() in urls patterns
- Python TypeError on regex [duplicate]