Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've an issue here. I have a string and I want to extract parts of it using regex. Here is the string
{{name}} I love me some work {{hero}}
I want to extract
[{{name}}, {{hero}}]
also in a case where the string exist as
{{name} I love me some work {{hero, come in {here, this is right}
I still want to get
[{{name}, {{name, {here, right}]
I hope this makes sense. I am working with Python.
If you want ['{{name}}', '{{hero}}', '{{hero, come in {here, this is right}'] use #Avinash's regex.
If you want ['{{name}}', '{{hero}}', '{{hero', {here', 'right}'] use the following:
re.findall(r'{+\w+}*|{*\w+}+', s)
RegEX DEMO
Have you tried the following?
import re
s = '{{name} I love me some work {{hero, come in {here, this is right}'
print re.findall(r'\{.*?\}', s)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have a string that might have any of the following format (example) :
1111__1111
1111__1111_11
111_11A_11
I have added the following check :
import re
print(bool(re.match("\d__\d","1111_1111"))
print(bool(re.match("\d__\d_\d","1111_1111_11"))
print(bool(re.match("\d_\d[A-Za-z]_\d","111_11A_11"))
I don't think the regex is correct because when I introduce a character in the first regex for example it returns me True Always.
can you please point me to a solution?
Thank you
It returns True because the pattern is trying to find matches based on each one of the characters inside the pattern string.
The following regular expression finds exact matches for the three scenarios:
print(bool(re.match("(^\d{4}__\d{4}$)","1111__1111")))
print(bool(re.match("(^\d{4}\_\d{4}\_\d{2}$)","1111_1111_11")))
print(bool(re.match("(^\d{3}_\d{2}[A-Z]_\d{2}$)","111_11A_11")))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is one of the string that I got:
str ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
what I want:
['Name', 'coordinates','CITIZENS','colonisation']
I'm trying to get word in string such as Name, coordinate, citizens, colonisation with their original case.
I tried split method to remove underscores and make them individual word.
,but it did not work well.
How can I do this?
Yo can use a regular expression for that:
import re
text ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
re.findall('_(\w*)_', text)
Note str is a built python function, don't use for variable names
A regex should do the trick:
import re
s = '_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
result = re.findall('_(\w+)_', s)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a text file in the following format:
Car: Replace:Brakes<10
Car: Renew: Engine=100000
Truck: Renew: Engine=1000
Truck: Replace: Brakes<504
I am looking to write a regex to parse this file and extract only the lines with Car in it and also only extract values after Car and return them as a python dictionary.
So my output would look like
'Replace' :' Brakes<10'
'Renew' : 'Engine=100000'
Any inputs on how I can achieve this?
I tried.
re.search
but get a re.Match object which I am not sure how to interpret.
Thank you!
There we go:
https://regex101.com/r/UTdN6B/1
use ^Car: (.*)|.* as pattern and \1 as substitute also gm as flags.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Ideally, I'd like the program to erase/un-log any and all erroneous character(s) when the Backspace key is pressed and replace them with the correct characters.
After searching for solutions to no avail, I'm wondering if it's even possible? If it is, my guess is that the code needed to do this might involve the modules: 're', 'readchar', 'msvcrt', 'getch' or some combination of those, in addition to using 'string.replace', 'x.remove', 'r/R', 'raw_input' 'x.translate', or the like. But I don't have the knowledge or skills yet to figure out how to apply them.
This code may be what you are looking for:
import re
text = "Helll[Back Space]o how are yoo[Back Space]u"
result = list(text)
for (start, end) in [(m.start(), m.end()) for m in re.finditer('\[Back Space\]', text)]:
text = text.replace(''.join(result[start-1:end]), '')
print text
Output:
Hello how are you
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am newbie in grep and I'm familiar with Python. My problem is to find and replace every string inside the quote like "text" by < em >text< /em >
The source file has the html form
Thanks
That'll do the trick
import re
s = '"text" "some"'
res = re.subn('"([^"]*)"', '<em>\\1</em>', s)[0]