Reputation: 4991
I am trying to remove all digits from a string that are not attached to a word. Examples:
"python 3" => "python"
"python3" => "python3"
"1something" => "1something"
"2" => ""
"434" => ""
"python 35" => "python"
"1 " => ""
" 232" => ""
Till now I am using the following regular expression:
((?<=[ ])[0-9]+(?=[ ])|(?<=[ ])[0-9]+|^[0-9]$)
which can correctly do some of the examples above, but not all. Any help and some explanation?
Upvotes: 2
Views: 14085
Reputation: 180391
You could just split the words and remove any words that are digits which is a lot easier to read:
new = " ".join([w for w in s.split() if not w.isdigit()])
And also seems faster:
In [27]: p = re.compile(r'\b\d+\b')
In [28]: s = " ".join(['python 3', 'python3', '1something', '2', '434', 'python
...: 35', '1 ', ' 232'])
In [29]: timeit " ".join([w for w in s.split() if not w.isdigit()])
100000 loops, best of 3: 1.54 µs per loop
In [30]: timeit p.sub('', s)
100000 loops, best of 3: 3.34 µs per loop
It also removes the space like your expected output:
In [39]: re.sub(r'\b\d+\b', '', " 2")
Out[39]: ' '
In [40]: " ".join([w for w in " 2".split() if not w.isdigit()])
Out[40]: ''
In [41]: re.sub(r'\b\d+\b', '', s)
Out[41]: 'python python3 1something python '
In [42]: " ".join([w for w in s.split() if not w.isdigit()])
Out[42]: 'python python3 1something python'
So both approaches are significantly different.
Upvotes: 3
Reputation: 8254
Why not just use word boundaries?
\b\d+\b
Here is an example:
>>> import re
>>> words = ['python 3', 'python3', '1something', '2', '434', 'python 35', '1 ', ' 232']
>>> for word in words:
... print("'{}' => '{}'".format(word, re.sub(r'\b\d+\b', '', word)))
...
'python 3' => 'python '
'python3' => 'python3'
'1something' => '1something'
'2' => ''
'434' => ''
'python 35' => 'python '
'1 ' => ' '
' 232' => ' '
Note that this will not remove spaces before and after. I would advise using strip()
, but if not you can probably do \b\d+\b\s*
(for space after) or something similar.
Upvotes: 5
Reputation: 672
This regex, (\s|^)\d+(\s|$), could work as shown below in javascript
var value = "1 3@bar @foo2 * 112";
var matches = value.replace(/(\s|^)\d+(\s|$)/g,"");
console.log(matches)
It works in 3 parts:
You can replace $ with end of line or \n if you have several lines or just add it in next to it like this (\s|$|\n). Hope this is what your're looking for.
Upvotes: 0