This question already has an answer here:
use variable inside regex python
(1 answer)
Closed 5 years ago.
EDIT: So apparently, this is a string interpolation in regex. Thanks for clarifying.
I have a input function that is named omj.
omj
When I run it, it gives me
"obik"
I then use that output in this regex function
re.findall("\w*obik\w*",dataframe)
I received EXACTLY what I wanted, which is the answer
"Yaobikuni"
Notice that "obik" is in the word, and there is only one match for it. Is there a way to put the input omj in the regex function to get Yaobikuni straightforwardly, or is this the only way it would work?
EDIT: I don't understand why people are downvoting, but I thought I made it clear that omj can be considered as an input string that gives the answer obik.
omj = """obik"""
EDIT2: Thank you for the help #Nick Chapman . I tried this on the first time and I thought it might not have been possible to infuse the input omj on an regex function:
re.findall("\w*"omj"\w*",dataframe)
The first argument is just a string, so your real question is how to do string interpolation in Python.
How about this:
re.findall("\w*{}\w*".format(omj()), dataframe)
Can't you just do
re.findall("\w*" + omj + "\w*",dataframe)
Related
This question already has answers here:
Pythonic way to replace chars
(2 answers)
Closed 1 year ago.
I have a problem when trying to switch the signs of all arithmetic operations inside a string in Python.
My input looks like this: "+1-2"
And the output should be something like that: "-1+2"
But when I try to replace the characters with replace() function:
"+1-2".replace("+", "-").replace("-", "+")
The output I get is that: '+1+2'
Looks like the replace functions are replacing everything at the same time, so it will never switch it correctly in that way. A one-liner solution with similar functions would be very helpful.
Use str.translate:
s = "+1-2+3+4-2-1"
t = str.maketrans('+-','-+')
print(s.translate(t))
Output:
-1+2-3-4+2+1
This question already has answers here:
re.findall behaves weird
(3 answers)
Closed 3 years ago.
I want to achieve the following:
say I have two regex, regex1 and regex2. I want to construct a new regex that is of 'prefix_regex1 | prefix_regex2', what syntax should I use to share the prefix, I tried 'prefix_(regex1|regex2)' but it's not working, since I think it's confused on the bracket used as group rather than making the | precedence higher.
example:
I have two string that both should match the pattern:
prefix_123
prefix_abc
I wrote this pattern: prefix_(\d*|\D*) that tries to capture both cases, but when I run it against prefix_abc it's only matching prefix_, not the entire string.
This site might help with this problem (and others). It lets you tinker with the regex and see the result both graphically and in code: https://www.debuggex.com/
For example, I changed your regex to this: prefix_(\d+|\D+) which requires 1 or more digit or non-digit after "prefix_" Not sure if that's what you are looking for, but it's easy to experiment with the site I shared above.
Hope it helps.
This question already has an answer here:
How can I find all matches to a regular expression in Python?
(1 answer)
Closed 3 years ago.
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??
Try this:
import re
s = "bbccaa"
print(re.sub("[^ab]+", r"", s))
#bbaa
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)
Since we are practicing here, another expression, not the best one, would be:
([ab]+)|(.+?)
Demo
This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 4 years ago.
I have written a code that ends up outputting what I want but in list format. Just to make it easier to understand, I will make up an input.
If I get
>>>
['H','e','l','l','o',' ','W','o','r','l','d']
as an output, how can I change it to:
>>>
'Hello World'
I have tried using .join() but it tells me that it does not work with lists as an error code.
If you need any more information, or I am being vague, just leave a comment saying so and I will update the question.
And if you leave a downvote, can you at least tell me why so that I can fix it or know what to improve for later posts
You join on the connector like this: ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
Just use join method by passing a list as parameter.
str = ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
It's really odd but I have this string:
"['please', 'help']"
I want something that would get one argument at a time.
I've searched everywhere for this but I didn't find anything.
Thanks in advance
While eval is a correct approach, it can often have negative consequences. I suggest using ast.literal_eval, which is a more safe approach (as mentioned by the linked docs):
import ast
s = "['please', 'help']"
s_list = ast.literal_eval(s)
print s_list
Are you looking for something like this?
string = "['please', 'help']"
string_list = eval(string)
print string_list[0], string_list[1]
Edit: you should ideally use ast.literal_eval as the other answer suggests, if you are unsure of what the string contains.