Why won't this regex work? [duplicate] - python

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
Closed 5 years ago.
Im try to match ip addresses and have come up with the following regular expression for python. I just cannot understand why this wont work. Any help would be greatly appreciated!
r"[0-255]\.[0-255]\.[0-255]\.[0-255]"

Because [0-255] means any char between 0 to 2 or 5. switch to something like
r"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

Related

How to get the number of letters of a word in python? [duplicate]

This question already has answers here:
How to get the size of a string in Python?
(6 answers)
Closed 1 year ago.
This is what have tried but it's not working
lens("Father")
it's len('Father') not lens('Father')
len(Father)
Your mistake is the 's' at the end of len's'

reversing a section of string in python with [start:stop:step] [duplicate]

This question already has answers here:
Understanding negative steps in list slicing
(2 answers)
Closed 1 year ago.
I am learning python and I can't solve this problem:
I am ok to reverse the whole string but I want to get only the "Hello" part
astring = "Hello world!"
I was expecting print(astring[0:4:-1]) would do the work but it does not.
print(astring[5:0:-1]) is better but the H is still missing. I get "olle"
Is there anyway to solve this?
Thank you,
Try this:
print(astring[4::-1])

get strings between 2 delimiter in python [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I would like to get, from the following string "/path/to/%directory_1%/%directory_2%.csv"
the following list: [directory_1, directory_2]. I would like to avoid using split by "%" my string. I was hoping to find a regex that could help me. However I cannot find the correct one.
For now, I have the following:
re.findall('%(.*)%', dirty_arg)
which output ["directory_1%/%directory_2"]
Do you have any recommandation about that?
Thank you very much for your help.
Try this:
import re
regex = r"%(.*?)%"
dirty_arg = "/path/to/%directory_1%/%directory_2%.csv"
print(re.findall(regex, dirty_arg))
I've added ? to your regex which makes sure it matches as few times as possible. The output of this code is ['directory_1', 'directory_2']

What are () (parentheses) are for in regex python [duplicate]

This question already has answers here:
Python regex -- extraneous matchings
(5 answers)
Closed 6 years ago.
I searched in all the internet and didnt get a good answer on this thing.
What parentheses in python are stand for? its very wierd..
For example, if i do:
re.split(r'(/s*)', "ho from there")
its will give me a list of separate words with the spaces between that... how does its happening?
This isn't specific to python, but in regex those denote a capture group.
Further information on how these are handled in re.split can be seen here

Explain this python string operation s[:6][::-1] [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
I am new to python. Please anybody explain the following string operations
s="abcdefghijklmnop"
print s[:6][::-1] #is it first calculating s[:6] and then operating the result with [::-1] ?
"abcdefghijklmnop"[:6][::-1]
Take first 6 characters (abcdef).
Read the result from the end to the beginning (fedcba).
There is an other better way to get this result:
"abcdefghijklmnop"[5::-1]

Categories