I have a string that contains sub strings like
RTDEFINITION(55,4) RTDEFINITION(45,2)
I need to replace every occurrence of this kind of string with another string:
DEFRTE
using Python and regular expressions. Any ideas?
thx
This should work
import re
re.sub(r'RTDEFINITION\(\d+,\d+\)', 'DEFRTE', mystring)
Related
I am trying to remove all elements that have the form \x followed by two numbers. I have created the following regular expression r'\\x[0-9][0-9]'. I then test it with the following code:
pattern1 = r'\\x[0-9][0-9]'
a = "\x85ciao \x85839"
re.sub(pattern1, "", a)
But it is not working as it does not replace anything. The output is in fact the same as string a. What could be causing this behaviour?
I am also having the same issue with replacing \' in strings. I would like to remove only the backlash and keep the '. How can I do this?
You can do it like this:
import re
a = "\x85ciao \x85839"
re.sub('\x85','', a)
or simply:
a.replace("\x85", "")
I have a string ="/One/Two/Three/Four"
I want to convert it to ="Four"
I can do this in one line in perl
string =~ s/.*+\///g
How Can I do this in python?
str_name="/One/Two/Three/Four"
str_name.split('/')[-1]
In general, split is a safe way to convert a string into a list based on some reg-ex. Then, we can call the last element in that list, which happens to be "Four" in this case.
Hope this helps.
Python's re module can handle regular expressions. For this case, you'd do
import re
my_str = "/One/Two/Three/Four"
new_str = re.sub(".*/", "", my_str)
# 'Four'
re.sub() is the regex replacement method. Like your perl regex, we simply look for any number of characters, followed by a slash, and then replace that with the empty string. What's left is what's after the last slash, which is 4.
The are alot of possibilities to solve this. One way would be by indexing the string. Other string method can be found here
string ="/One/Two/Three/Four"
string[string.index('Four'):]
Additionally you could split the string by the slash with .split('/')
print(string.split('/')[-1])
Another option would be regular expressions: see here
I need to extract only the part 0000520621 from the string nmg-22373-0000520621-001-010000520621.
I would like to use regular expressions in python for this task.
Can you help me in doing so?
You don't need a regexp to get the third member of a list. Just split your string by the minus character and pick it's third member.
test = 'nmg-22373-0000520621-001-010000520621'
test.split('-')[2]
my_string = 'nmg-22373-0000520621-001-010000520621'
expected = re.search('22373-(.+?)-001',l)
if expected:
print expected.group(1)
I need to match against a list of string values. I'm using '|'.join() to build a sting that is passed into re.match:
import re
line='GigabitEthernet0/1 is up, line protocol is up'
interfacenames=[
'Loopback',
'GigabitEthernet'
]
rex="r'" + '|'.join(interfacenames) + "'"
print rex
interface=re.match(rex,line)
print interface
The code result is:
r'Loopback|GigabitEthernet'
None
However if I copy past the string directly into match:
interface=re.match(r'Loopback|GigabitEthernet',line)
It works:
r'Loopback|GigabitEthernet'
<_sre.SRE_Match object at 0x7fcdaf2f4718>
I did try to replace .join with actual "Loopback|GigabitEthernet" in rex and it didn't work either. It looks like the pipe symbol is not treated as operator when passed from string.
Any thoughts how to fix it?
You use the r' prefix as a part of a string literal. This is how it could be used:
rex=r'|'.join(interfacenames)
See the Python demo
If the interfacenames may contain special regex metacharacters, escape the values like this:
rex=r'|'.join([re.escape(x) for x in interfacenames])
Also, if you plan to match the strings not only at the start of the string, use re.search rather than re.match. See What is the difference between Python's re.search and re.match?
You don't need to put "r'" at the beginning and "'". That's part of the syntax for literal raw strings, it's not part of the string itself.
rex = '|'.join(interfacenames)
I met some problems when I'd like to check if the input regex if correct or not.
I'd like to check is there one backslash before every symbol, but I don't know how to implement using Python.
For example:
number: 123456789. (return False)
phone\:111111 (return True)
I try to use (?!) and (?=) in Python, but it doesn't work.
Update:
I'd like to match the following string:
\~, \!, \#, \$, \%, \^, \&, \*, \(, \), \{, \}, \[, \], \:, \;, \", \', \>, \<, \?
Thank you very much.
import re
if re.seach(r"\\\W", "phone\:111111") is not None:
print("OK")
Does it work?
Reading between the lines a bit, it sounds like you are trying to pass a string to a regex and you want to make sure it has no special characters in it that are unescaped.
Python's re module has an inbuilt re.escape() function for this.
Example:
>>> import re
>>> print(re.escape("phone:111111"))
"phone\\:111111"
Check that the entire string is composed of single characters or pairs of backslash+symbol:
import re
def has_backslash_before_every_symbol(s):
return re.match(r"^(\\[~!#$%^&*(){}\[\]:;"'><?]|[^~!#$%^&*(){}\[\]:;"'><?])*$", s) is not None
Python regex reference: https://docs.python.org/3/library/re.html