I'm currently practicing regex. I declared--> str1="bbccaa"
. I want result to be all the b's and a's i.e 'bbaa'. I tried-> '[^c]+' ,[ab]+ But everything I tried ultimately gave an output as 'bb'. Can someone tell me where I'm going wrong and also the solution, please??
Asked
Active
Viewed 78 times
0
-
`s = s.replace('c', '')`? What are you really trying to do? – Wiktor Stribiżew Jun 16 '19 at 17:16
-
1Or replace with an empty string matching not a or b `[^ab]+` – The fourth bird Jun 16 '19 at 17:21
-
i declared str1="bbccaa", I wanted an output which gives an output of everything except the 'c' i.e an output which is "bbaa". How do i write a regex which gives me this output?? I tried using -->[^c]+ and [ab]+. But these werent giving me desired outputs. They gave me only "bb". – rookie Jun 16 '19 at 17:23
-
Except the `cc` - `re.sub(cc, replace_with_this, string_to_process)` – Wiktor Stribiżew Jun 16 '19 at 17:24
-
Ah thanks. But is there an expression i could write inside of re.search or re.match that could give me this same output? – rookie Jun 16 '19 at 17:26
-
So, the only problem was that you found a single match only with `re.search`. Use `re.findall` or `re.finditer` to match all the matches. – Wiktor Stribiżew Jun 16 '19 at 17:50
-
You declare? Does that mean you're using it as the target string for a regular expression ? New regex students usually aren't into the details of matching different parts of a string, but since you stipulate `[ab]+` I have to assume you understand capture groups. – Jun 16 '19 at 17:52
3 Answers
0
Try this:
import re
s = "bbccaa"
print(re.sub("[^ab]+", r"", s))
#bbaa

drec4s
- 7,946
- 8
- 33
- 54
-
Ah thanks. But is there an expression i could write inside of re.search or re.match that could give me this same output? – rookie Jun 16 '19 at 17:26
-
-
1
0

Emma
- 27,428
- 11
- 44
- 69
-
1re.search(([ab]+)|(.*?),str1)<-- did this. But im still getting --> 'bb' – rookie Jun 16 '19 at 17:31
-
2A little advice, never put `.*?` as the last construct in a regex. It will never, ever match a character. Therefore group 2 will always either be _NULL_ or the _empty_ string. – Jun 16 '19 at 17:47
0
I would use re.findall
for that, then join
results following way:
import re
str1="bbccaa"
output = ''.join(re.findall('a|b',str1))
print(output)
Output:
bbaa
I do not see way to make it solely with re
(without join
)

Daweo
- 31,313
- 3
- 12
- 25