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”
- OR condition in Regex
- python .replace() regex [duplicate]
- What does \’.- mean in a Regular Expression
- How can I exclude one word with grep?
- Regex: ignore case sensitivity
- What does the regex \S mean in JavaScript?
- whitespace in regular expression
- How can I validate an email address using a regular expression?
- Regex: ignore case sensitivity
- Regular Expression for alphanumeric and underscores
- What is the difference between the regular expressions [^\d\s] and [\D\S]
- Regular Expression for alphanumeric and underscores
- re.sub erroring with “Expected string or bytes-like object”
- grep –ignore-case –only
- Regex AND operator
- Regular expression negative lookahead
- Regular Expression to match string starting with a specific word
- Regular Expressions: Is there an AND operator?
- Writing a very simple lexical analyser in C++
- Regular expression to match standard 10 digit phone number
- Regular Expression.how to add optional character at end of regex
- Regular Expression with wildcards to match any character
- 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
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- Regex – Does not contain certain Characters
- How to replace ” \ ” with ” \\ ” in java
- A regular expression to exclude a word/string
- Regex how to match an optional character
- How to validate an email address in JavaScript
- Using Bash regex match (=~) where regex includes quotes (” characters)
- Searching for UUIDs in text with regex
- Regular expression for exact match of a string
- Using the star sign in grep
- Regex optional group
- Regex for string contains?
- Regular Expression – Validate Gmail addresses
- Remove all special characters, punctuation and spaces from string
- Java; String replace (using regular expressions)?
- 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?
- Which regular expression operator means ‘Don’t’ match this character?
- Negative matching using grep (match lines that do not contain foo)
- How to exclude a specific string constant?
- How to replace a whole line with sed?
- Validating IPv4 addresses with regexp
- How to validate phone number using PHP?
- Validate phone number with JavaScript
- Can a URL contain a semicolon and still be valid?
- Extract part of a regex match
- Regex to match only uppercase “words” with some exceptions
- Regular Expression to reformat a US phone number in Javascript
- Regex to match only letters
- Regex optional capturing group?
- Finding the complement of a DFA?
- TypeError: expected string or buffer
- Regular expressions in C: examples?
- How to ignore whitespace in a regular expression subject string?
- What does this Django regular expression mean? `?P`
- 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 limit number of characters to 10
- 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?
- Regex for “AND NOT” operation
- Using strip() to clean up a string
- Regular expression that matches valid IPv6 addresses
- Escaping Parentheses in Regex
- How to replace a string in an existing file in Perl
- python’s re: return True if string contains regex pattern
- What is the best regular expression to check if a string is a valid URL?
- Regex empty string or email
- Regex difference: (\w+)? and (\w*)
- Wrap element with in any number of elements with PHP
- extract shortcodes from string
- preg_replace and comment_form_defaults
- How to set up redirects for category paginated pages after adding /category/ to permalink
- Redirect non www to www using redirection plugin regex
- Restrict comments with regular expression
- How to fix old embeded iframes with WP-CLI search-replace
- Regex For Extracting First 4 letters of search term
- Use Regex Redirection via htsaccess for RSS Feeds WordPress