finding number in string REGEX [duplicate] - python

This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 9 years ago.
I am very new to regex and learning by practice. I wrote the following regex for finding a number inside a string of characters, however, it returns nothing. Why is that?
string = "hello world & bello stack 12456";
findObj = re.match(r'[0-9]+',string,re.I);
if findObj:
print findObj.group();
else:
print "nothing matched"
Regards

re.match must match from the beginning of the string.
Use re.search instead.

re.match matches from the start of the string. Use re.search
>>> my_string = "hello world & bello stack 12456"
>>> find_obj = re.search(r'[0-9]+', my_string, re.I)
>>> print find_obj.group()
12456
P.S semicolons are not necessary.

Related

Regular Expression to replace first occurance of match [duplicate]

This question already has answers here:
How to replace the first occurrence of a regular expression in Python?
(2 answers)
Closed 6 months ago.
Simple regex question. I have a string in the following format:
string = """陣頭には見るも<RUBY text="いかめ">厳</RUBY>しい、厚い鎧姿の武士達が立つ。
 分厚い鉄甲、長大な太刀――彼らの<RUBY text="かも">醸</RUBY>し出す威圧感
は、一騎のみでも背後の兵全てに優る戦力たり得ると
いう事実を、何より雄弁に物語っている。"""
What is the regular expression to find the first occurance of <RUBY text="something">something</RUBY> and replace it with something like HELLO i.e
 陣頭には見るもHELLOしい、厚い鎧姿の武士達が立つ。
 分厚い鉄甲、長大な太刀――彼らの<RUBY text="かも">醸</RUBY>し出す威圧感
は、一騎のみでも背後の兵全てに優る戦力たり得ると
いう事実を、何より雄弁に物語っている。
I tried it with (<R(.*?)/RUBY>){0} but this didn't work.
string = re.sub("(\<R(.*?)\/RUBY>){0}", "HELLO", string)
print(string)
Can be done like this:
string = """陣頭には見るも<RUBY text="いかめ">厳</RUBY>しい、厚い鎧姿の武士達が立つ。
 分厚い鉄甲、長大な太刀――彼らの<RUBY text="かも">醸</RUBY>し出す威圧感
は、一騎のみでも背後の兵全てに優る戦力たり得ると
いう事実を、何より雄弁に物語っている。"""
try:
first_match = re.findall(r'<RUBY text=.*</RUBY>', string)[0]
parts = string.split(first_match)
result = f'{parts[0]}HELLO{first_match.join(parts[1:])}'
except IndexError:
result = string
print(result)
Result:
陣頭には見るもHELLOしい、厚い鎧姿の武士達が立つ。
 分厚い鉄甲、長大な太刀――彼らの<RUBY text="かも">醸</RUBY>し出す威圧感
は、一騎のみでも背後の兵全てに優る戦力たり得ると
いう事実を、何より雄弁に物語っている。

How to replace the multiple different words with a single character/word in Python? [duplicate]

This question already has answers here:
Better way to remove multiple words from a string?
(5 answers)
Closed 3 years ago.
Note: Without chaining replace method (or) looping the characters in for loop (or) list comprehension
input_string = "the was is characters needs to replaced by empty spaces"
input_string.replace("the","").replace("was","").replace("is","").strip()
output: 'characters needs to replaced by empty spaces'
Is there any direct way to do this?
You can use python regex module(re.sub) to replace multiple characters with a single character:
input_string = "the was is characters needs to replaced by empty spaces"
import re
re.sub("the|was|is","",input_string).strip()
'characters needs to replaced by empty spaces'
This should help..
input_string = "the was is characters needs to replaced by empty spaces"
words_to_replace=['the', 'was','is']
print(input_string)
for words in words_to_replace:
input_string = input_string.replace(words, "")
print(input_string.strip())

Python re wrong output [duplicate]

This question already has answers here:
get index of character in python list
(4 answers)
Regular expression to match a dot
(7 answers)
Closed 3 years ago.
I want to find the position of '.', but when i run code below:
text = 'Hello world.'
pattern = '.'
search = re.search(pattern,text)
print(search.start())
print(search.end())
Output is:
0
1
Place of '.' isn't 0 1.
So why is it giving wrong output?
You can use find method for this task.
my_string = "test"
s_position = my_string.find('s')
print (s_position)
Output
2
If you really want to use RegEx be sure to escape the dot character or it will be interpreted as a special character.
The dot in RegEx matches any character except the newline symbol.
text = 'Hello world.'
pattern = '\.'
search = re.search(pattern,text)
print(search.start())
print(search.end())

Python's .group() returning only the first match [duplicate]

This question already has an answer here:
re.search() only matches the first occurrence
(1 answer)
Closed 3 years ago.
I ran the following code and get only the first ')' as a match. Could someone help me with why the regular greedy '))' is not being returned?
r=re.compile('\)')
var=r.search('- hi- ))there')
print var.group()
search will only return the first match.
To find all matches use findall:
r=re.compile('\)')
var= r.findall('- hi- )) there')
print (var)
If you want to find both braces in one match use:
r=re.compile('\)+')
The + matches to 1 or more of the object.
Your regex isn't greedy. In fact, it's set up to match only a single character. If you want it to match repeats as well, add a +:
>>> r=re.compile('\)+')
>>> var=r.search('- hi- ))there')
>>> print var.group()
))

remove unwanted space in between a string [duplicate]

This question already has answers here:
Is there a simple way to remove multiple spaces in a string?
(27 answers)
Closed 6 years ago.
I wanna know how to remove unwanted space in between a string. For example:
>>> a = "Hello world"
and i want to print it removing the extra middle spaces.
Hello world
This will work:
" ".join(a.split())
Without any arguments, a.split() will automatically split on whitespace and discard duplicates, the " ".join() joins the resulting list into one string.
Regular expressions also work
>>> import re
>>> re.sub(r'\s+', ' ', 'Hello World')
'Hello World'

Categories