Use a negative lookahead and a negative lookbehind:
> s = "one two 3.4 5,6 seven.eight nine,ten" > parts = re.split('\s|(?<!\d)[,.](?!\d)', s) ['one', 'two', '3.4', '5,6', 'seven', 'eight', 'nine', 'ten']
In other words, you always split by \s
(whitespace), and only split by commas and periods if they are not followed (?!\d)
or preceded (?<!\d)
by a digit.
DEMO.
EDIT: As per @verdesmarald comment, you may want to use the following instead:
> s = "one two 3.4 5,6 seven.eight nine,ten,1.2,a,5" > print re.split('\s|(?<!\d)[,.]|[,.](?!\d)', s) ['one', 'two', '3.4', '5,6', 'seven', 'eight', 'nine', 'ten', '1.2', 'a', '5']
This will split "1.2,a,5"
into ["1.2", "a", "5"]
.
DEMO.
Related Posts:
- Python regex AttributeError: ‘NoneType’ object has no attribute ‘group’
- python re.split() to split by spaces, commas, and periods, but not in cases like 1,000 or 1.50
- Split string on whitespace in Python
- Python string.replace regular expression
- Python string.replace regular expression
- Grep and Python
- What is the grep equivalent in Python?
- Python re.split() vs split()
- python regular expression “\1”
- Using or ‘|’ in regex [duplicate]
- What is a Context Free Grammar?
- python .replace() regex [duplicate]
- What does \’.- mean in a Regular Expression
- How can I exclude one word with grep?
- How can I validate an email address using a regular expression?
- Regular expression to match a line that doesn’t contain a word
- Difference between \b and \B in regex
- Regex: ignore case sensitivity
- whitespace in regular expression
- Regular Expression for alphanumeric and underscores
- What is the difference between the regular expressions [^\d\s] and [\D\S]
- Regex: AttributeError: ‘NoneType’ object has no attribute ‘groups’
- Regular Expression for alphanumeric and underscores
- grep –ignore-case –only
- Regex AND operator
- Regex to test if string begins with http:// or https://
- Regular Expression to match string starting with a specific word
- Regular Expressions: Is there an AND operator?
- Regular expression to match standard 10 digit phone number
- What is a non-capturing group in regular expressions?
- How to find all occurrences of a substring?
- Does “\d” in regex mean a digit?
- How to validate an email address in PHP
- What is a non-capturing group in regular expressions?
- Regular expression to match standard 10 digit phone number
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- Remove all special characters, punctuation and spaces from string
- This can be done without regex:
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- Symbol for any number of any characters in regex?
- Regex – Does not contain certain Characters
- Regex how to match an optional character
- How to validate an email address in JavaScript
- Using Bash regex match (=~) where regex includes quotes (” characters)
- Regular expression to match a word or its prefix
- How can I remove punctuation from input text in Java?
- Regular expression for exact match of a string
- Using the star sign in grep
- Regex for string contains?
- Difference between \w and \b regular expression meta characters
- Regular Expression – Validate Gmail addresses
- How can I remove punctuation from input text in Java?
- How can I output only captured groups with sed?
- How to negate specific word in regex?
- javascript HTML from document.body.innerHTML
- Which regular expression operator means ‘Don’t’ match this character?
- How can I write a regex which matches non greedy?
- Regex Explanation ^.*$ [duplicate]
- Negative matching using grep (match lines that do not contain foo)
- How to replace a whole line with sed?
- How to validate phone number using PHP?
- Can a URL contain a semicolon and still be valid?
- How is \\n and \\\n interpreted by the expanded regular expression?
- Regular Expression to reformat a US phone number in Javascript
- Regex to match only letters
- Finding the complement of a DFA?
- RegEx to find two or more consecutive chars
- RE error: illegal byte sequence on Mac OS X
- How to remove parentheses from string [duplicate]
- Regex to match an optional ‘+’ symbol followed by any number of digits
- TypeError: expected string or buffer
- Regex Match all characters between two strings
- Regular expression to match a word or its prefix
- Regular expressions in C: examples?
- How to ignore whitespace in a regular expression subject string?
- Regular expression for a string that does not start with a sequence
- Regex: matching up to the first occurrence of a character
- Regex credit card number tests
- Regular expression – starting and ending with a character string
- Regular expression to stop at first match
- /bb|[^b]{2}/ how does it work?
- How do I match any character across multiple lines in a regular expression?
- Regex that matches integers in between whitespace or start/end of string only
- Regular Expressions- Match Anything
- What is the difference between square brackets and parentheses in a regex?
- My Vim replace with a regex is throwing a `E488: Trailing characters`
- Using strip() to clean up a string
- Python regex match space only
- Case insensitive regular expression without re.compile?
- How to replace a string in an existing file in Perl
- Regex empty string or email
- Regex difference: (\w+)? and (\w*)
- extract shortcodes from string
- preg_replace and comment_form_defaults
- 404 regular-expression advice needed
- How to set up redirects for category paginated pages after adding /category/ to permalink
- Adding span tags to post titles using regex
- Call Shortcode Attribute Value within another function
- Regex For Extracting First 4 letters of search term
- Use Regex Redirection via htsaccess for RSS Feeds WordPress