2

I have a string that I want to pull a substring from. Currently I'm using 3 lines to achieve this as follows:

for z in my_list:
    abc = re.sub('\n', '', z[0])
    xyz = re.sub('SNMPv2-SMI::', '', abc)
    egg = re.sub('\..*\ =.*$', '', xyz)

However, as this is using 3 separate regex commands, is there a way to get the string using a single statement/command?

1
  • If you are gonna do this i would just use the same name each line or you will run out of ideas eventually. Commented Jul 10, 2012 at 6:40

4 Answers 4

2

Just use the | ("or") operator:

strng = re.sub(r'(this)|(that)|(something else)', '', strng)
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this may give different behavior in certain cases. The code in the original post will replace occurrences of later regexes that are created by removing earlier ones. For instance, if the input string contains "SNMPv2-\nSMI::", the first replace will remove the \n, and the second will remove the rest. Using one regex won't do this. This is probably a good thing, but it's worth noting the difference.
2

as the replacement string is same you can use |.

>>> import re
>>> s="this is for testing"
>>> re.sub("this|for|test","REPLACED",s)
'REPLACED is REPLACED REPLACEDing'

Comments

0
for z in my_list:
    abc = re.sub('\..*\ =.*$', '', re.sub('SNMPv2-SMI::', '', re.sub('\n', '', z[0]))) 

Ugly little one-liner for you

6 Comments

... does this reduced the command used ? and it did nothing but make this more difficult to read.
it reduces the number of statements to one. Of course this is ugly, I even said so in my answer. I would keep the original format instead of trying to substitute everything into a long regex. Regexes are a pain to read for most people, let's not make it into something that makes your maintainence dev hate you
Rather then find the string and changing it to nothing. In order to leave the original string, is there no way to match a string within a line of text and then assign this match to a variable ?
when you say xyz = ... all you are doing is storing a variable. All my oneliner did was to insert the content of the variable directly into your re.sub calls without storing them. If you MUST, then feel free to use my answer, but I think it's pretty obvious how ugly my solution is, which is why I urge you to use your original solution, just with a better naming scheme
Sure I appreciate what you are saying but is there no way to issue a regex match rather the sub ?
|
0


You can use silce tips for delete the fisrt character :

  z[1:]

And like the others say you can make a logical OR with '(a|b|c)'. I think your code like that :

for z in my_list:
    egg = re.sub( r'(SNMPv2-SMI::|\..*\ =.*)', '', z[1:] )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.