This question already has answers here:
Match exact string
(3 answers)
Closed 4 years ago.
a sentence needs to contain 1 or more instances of 'a', exactly 1 instance of 'b' and 0 or more instances of 'c'
my expression is a+bc*
it works for strings like 'abc' 'ab' 'aabcc' which is all fine but it also works when i have multiple b's like 'abbc' which it shouldn't. How do i get it to work when theres only 1 'b'
Here is my full code
import re
qq = re.compile('a+bc*')
if qq.match('abb') is not None:
print("True")
else:
print('False')
which should produce False
Use qq=re.compile(r'^a+bc*$'). The ^ means match at start and $ means match at the end.
You want to match the pattern to the full string and not a part of it. That is why you need the ^ and $ in this case
Related
This question already has answers here:
Count the number of occurrences of a character in a string
(26 answers)
Closed 25 days ago.
Let's say I have a string:
my_sentence = "I like not only apples, but also bananas."
and I would like to use python regex to match string if for example a letter occurs at least 3 times in it (no matter on what place). How will pattern look like in this case?
You could use
import re
matches = re.findall(r'a', my_sentence) # ['a', 'a', 'a', 'a', 'a']
And then check for the number of occurrences
len(matches) > 3 # True
Try this pattern:
^.*(a.*){3}$
You can easily change it to any number of a's or any other character.
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())
This question already has answers here:
Count the number of occurrences of a character in a string
(26 answers)
Closed 3 years ago.
how do you check if a string has more than one specific character in python. Example The string, 'mood' would clearly have two 'o' characters
You can use the str.count method:
>>> 'mood'.count('o') > 1
True
>>>
This question already has answers here:
Python replace string pattern with output of function
(4 answers)
Closed 5 years ago.
Say I have the following string:
mystr = "6374696f6e20????28??????2c??2c????29"
And I want to replace every sequence of "??" with its length\2. So for the example above, I want to get the following result:
mystr = "6374696f6e2022832c12c229"
Meaning:
???? replaced with 2
?????? replaced with 3
?? replaced with 1
???? replaced with 2
I tried the following but I'm not sure it's the good approach, and anyway -- it doesn't work:
regex = re.compile('(\?+)')
matches = regex.findall(mystr)
if matches:
for match in matches:
match_length = len(match)/2
if (match_length > 0):
mystr= regex .sub(match_length , mystr)
You can use a callback function in Python's re.sub. FYI lambda expressions are shorthand to create anonymous functions.
See code in use here
import re
mystr = "6374696f6e20????28??????2c??2c????29"
regex = re.compile(r"\?+")
print(re.sub(regex, lambda m: str(int(len(m.group())/2)), mystr))
There seems to be uncertainty about what should happen in the case of ???. The above code will result in 1 since it converts to int. Without int conversion the result would be 1.0. If you want to ??? to become 1? you can use the pattern (?:\?{2})+ instead.
This question already has answers here:
re.sub replace with matched content
(4 answers)
Closed 8 years ago.
I want to replace the digits in the middle of telephone with regex but failed. Here is my code:
temp= re.sub(r'1([0-9]{1}[0-9])[0-9]{4}([0-9]{4})', repl=r'$1****$2', tel_phone)
print temp
In the output, it always shows:
$1****$2
But I want to show like this: 131****1234. How to accomplish it ? Thanks
I think you're trying to replace four digits present in the middle (four digits present before the last four digits) with ****
>>> s = "13111111234"
>>> temp= re.sub(r'^(1[0-9]{2})[0-9]{4}([0-9]{4})$', r'\1****\2', s)
>>> print temp
131****1234
You might have seen $1 in replacement string in other languages. However, in Python, use \1 instead of $1. For correctness, you also need to include the starting 1 in the first capturing group, so that the output also include the starting 1; otherwise, the starting 1 will be lost.