Converting a string variable to a regular expression in python [duplicate] - python

This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 2 years ago.
I am creating a python function with two inputs: a file and a string, in which user can find the location of the string in the file. I figured the best way to do this would be with regular expressions. I have converted the file to one big string (file_string) earlier in the code. For example, let's say the user wants to find "hello" in the file.
input = "hello"
user_input = "r'(" + input + ")'"
regex = re.compile(user_input)
for match in regex.finditer(file_string):
print(match.start())
Creating a new string with r' ' around the input variable is not working. However, the code works perfectly if I replace user_input with r'hello'. How can I convert the string input the user enters to an expression that can be put into re.compile()?
Thanks in advance.

The r is just part of the syntax for raw string literals, which are useful for simplifying some regular expressions. For example, "\\foo" and r'\foo' produce the same str object. It is not part of the value of the string itself.
All you need to do is create a string with the value of input between ( and ).
input = "hello"
user_input = "(" + input + ")"
More efficiently (if only marginally so)
user_input = "({})".format(input)
Or more simply in recent versions of Python:
user_input = f'({input})'

Related

How I read a word after the # symbol [duplicate]

This question already has answers here:
How to extract the substring between two markers?
(22 answers)
Closed last month.
I'm having a problem. I need to create the #everyone_or_person feature. A bit like discord. But I'll have to be able to read the word after the # and stop reading when there is a ("SPACE"/"_") and check for that word in the list. I've appended a simple version as an example. I knew it would not work but I couldn't think of anything else.
input = input("input: ")
value = input.find("#")
output = input.partition("#")[0]
print(str(output))
I've tried to look up how to do it but to no avail.
simply use split:
test = "Some input with #your_desired_value in it"
result = test.split("#")[1].split(" ")[0]
print(result)
this splits your text at the #, takes the entire string after the #, splits again at the first space, and takes the string before that.

Remove spaces and colon from a string [duplicate]

This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 1 year ago.
I'm trying to remove all the spaces and colon from a given string, using a single function, but I'm unable to achieve these two things in a single function can someone please help me on this?
def normalize_string1(string):
return string.replace(" ", "")
def normalize_string2(string):
return string.replace(":- ", "-")
normalize_string("AIX Server--1uryeu6438shdj:-thsoanfg_321-aq.com")
Write This code for your string. It will definitely work. This is a very common mistakes developer makes while working with strings. Here I am taking a string as an example. You can also do both tasks using a single function using this code.
global string
string = "Hello Everyone :- from YOURNAME"
def dtring(string):
string = string.replace(" ", "")
string = string.replace(":-", "-")
print(string)
dtring(string=string)
I used print statement to show you the changes.
One of the major mistakes was you just changed the value of the string but you had not updated those changes to your original string. You can update that value by using the "string =" statement. I hope this is helpful for you. You may try this program with any string

python split a string with special charcter [duplicate]

This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 1 year ago.
I need to split a string that I receive like that :
my_string = "\data\details\350.23.43.txt"
when I use my_string.replace ("\\", "/")
it returns : /data/detailsè.23.43.txt
It's considering the \350 in my string as a special character 'è'
Edit after your comment:
Try
my_string = r"\data\details\350.23.43.txt"
That happens because \ooo is interpreted as a character with octal value as described in the docs.
I guess the only way is to escape the \ as in:
my_string = "\data\details\\350.23.43.txt"
Then you can do stuff like:
my_string.split("\\")
Where do you get the string from? Is there a way to influence that?
And this looks like a path. It would be better to use
os.path.join("data", "details", "350.23.43.txt")
to create paths independently of the operating system.
\ in string literals are treated as escaping chars. That is why s1 = "line\nsecond line" creates a string with two lines. That is also why you use "\\" in my_string.replace ("\\", "/").
So to fix your problem, if you're using a string literal my string = "\data\details\350.23.43.txt" you should instead use "\\data\\details\\350.23.43.txt" to make sure your \ are properly escaped. Alternatively, you can use a raw string my string = r"\data\details\350.23.43.txt" by prepending r to the quote. That way nothing gets escaped (so r"\n" would be a 2 char string with \ and n instead of just a single new line char)

re.sub() doesn't replace middle of string [duplicate]

This question already has answers here:
How to replace only the contents within brackets using regular expressions?
(2 answers)
Closed 6 years ago.
I am trying to replace the contents of brackets in a string with nothing. The code I am using right now is like this:
tstString = "OUTPUT:TRACK[:STATE]?"
modString = re.sub("[\[\]]","",tstString)
When I print the results, I get:
OUTPUT:TRACK:STATE?
But I want the result to be:
OUTPUT:TRACK?
How can I do this?
I guess this one will work fine. Regexp now match Some string Inside []. Not ? after *. It makes * non-greedy
import re
tstString = "OUTPUT:TRACK[:STATE]?"
modString = re.sub("\[.*?\]", "", tstString)
print modString
Your regular expression "[\[\]]" says 'any of these characters: "[", "]"'.
But you want to delete what's between the square brackets too, so you should use something like r"\[:\w+\]". It says '[, then :, then one or more alphanumeric characters, then ]'.
And please, always use raw strings (r in front of quotes) when working with regular expressions to avoid funny things connected with Python string processing.

In Python, is there a way to validate a user input in a certain format? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
In python, I'm asking the user to input an office code location which needs to be in the format: XX-XXX (where the X's would be letters)
How can I ensure that their input follows the format, and if it doesn't ask them to input the office code again?
Thanks!
The standard (and language-agnostic) way of doing that is by using regular expressions:
import re
re.match('^[0-9]{2}-[0-9]{3}$', some_text)
The above example returns True (in fact, a "truthy" return value, but you can pretend it's True) if the text contains 2 digits, a hyphen and 3 other digits. Here is the regex above broken down to its parts:
^ # marks the start of the string
[0-9] # any character between 0 and 9, basically one of 0123456789
{2} # two times
- # a hyphen
[0-9] # another character between 0 and 9
{3} # three times
$ # end of string
I suggest you read more about regular expressions (or re, or regex, or regexp, however you want to name it), they're some kind of swiss army knife for a programmer.
In your case, you can use a regular expression:
import re
while True:
inp = input() # raw_input in Python 2.x
if re.match(r'[a-zA-Z0-9]{2}-[a-zA-Z0-9]{3}$', inp):
return inp
print('Invalid office code, please enter again:')
Note that in many other cases, you can simply try converting the input into your internal representation. For example, when the input is a number, the code should look like:
def readNumber():
while True:
try:
return int(input()) # raw_input in Python 2.x
except ValueError:
pass

Categories

Resources