This question already has answers here:
Python extract pattern matches
(10 answers)
Closed 1 year ago.
I made this code:
import re
match = re.search(r'[DER]\d+[Y]', 'DER1234Y' )
print(match.group())
and it prints this :
R1234Y
I want the code to only print the numbers and nothing else. How to do that ?
It's basically regex. So would this work?: re.sub('[^0-9]+', '', 'DER1234Y')
[^0-9]+ = everything that is not a numeric value (0-9).
Related
This question already has answers here:
Regular Expressions: Is there an AND operator?
(14 answers)
Closed 2 years ago.
I have a list of strings like:
1,-102a
1,123-f
1943dsa
-da238,
-,dwjqi92
How can I make a Regex expression in Python that matches as long as the string contains the characters , AND - regardless of the order or the pattern in which they appear?
I would use the following regex alternation:
,.*-|-.*,
Sample script:
inp = ['1,-102a', '1,123-f', '1943dsa', '-da238,', '-,dwjqi92']
output = [x for x in inp if re.search(r',.*-|-.*,', x)]
print(output)
This prints:
['1,-102a', '1,123-f', '-da238,', '-,dwjqi92']
This question already has answers here:
How to remove substring from string in Python 3
(2 answers)
Closed 2 years ago.
Is there I way to delete words from a string in Python if it doesn't have spaces. For example, if you have the string "WUBHELLOWUB" I want to remove "WUB". I tried
s = 'WUBHELLOWUB'
while 'WUB' in s:
ind = s.find('WUB')
s = s[:ind] + s[ind+1:]
print(s)
but it did not work.
You can use regex
import re
data=r"\S*WUB\S*"
re.sub(data, '','WUBWUBHELLO')
This question already has answers here:
re.findall behaves weird
(3 answers)
Closed 2 years ago.
I'm trying to find numbers in a string.
import re
text = "42 ttt 1,234 uuu 6,789,001"
finder = re.compile(r'\d{1,3}(,\d{3})*')
print(re.findall(finder, text))
It returns this:
['', ',234', ',745']
What's wrong with regex?
How can I get ['42', '1,234', '6,789,745']?
Note: I'm getting correct result at https://regexr.com
You indicate with parentheses (...) what the groups are that should be captured by the regex.
In your case, you only capture the part after (and including) the first comma. Instead, you can capture the whole number by putting a group around everything, and make the parentheses you need for * non-capturing through an initial ?:, like so:
r'(\d{1,3}(?:,\d{3})*)'
This gives the correct result:
>>> print(re.findall(finder, text))
['42', '1,234', '6,789,001']
you just need to change your finder like this.
finder = re.compile(r'\d+\,?\d+,?\d*')
This question already has answers here:
Python strip unexpected behavior
(2 answers)
Closed 4 years ago.
I would like to strip the ending from a string, as in following:
da = "abc.com"
print(da.strip(".com"))
My expected outcome is abc. However, ab is returned instead.
Why is that and how to solve it?
this should work: simply split string by . and discard the last piece.
print(da.split('.')[:-1])
Using Regex.
import re
da = "abc.com"
print(re.sub("\.com", "", da))
or
print(da.replace(".com", ""))
Output:
abc
This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 5 years ago.
I am working with Python 3 and I want to replace the emoticons included in a dictionary.
For example
text = "Hi, I'm coming home :)"
#Create dictionary
dict_lookup = {':(' : 'sad',
':)' : 'happy'}
The desired output is:
Hi, I'm coming home happy
What is the most efficient way to achieve this result in Python 3?
This should do the trick:
for emote, replacement in dict_lookup.items():
text = text.replace(emote, replacement)
Take a look at str.replace
It allows you to do text.replace(dict_key, dict_value)