0

I am trying to write a generic pattern using regex so that it fetches only particular things from the string. Let's say we have strings like GigabitEthernet0/0/0/0 or FastEthernet0/4 or Ethernet0/0.222. The regex should fetch the first 2 characters and all the numerals. Therefore, the fetched result should be something like Gi0000 or Fa04 or Et00222 depending on the above cases.

x = 'GigabitEthernet0/0/0/2
m = re.search('([\w+]{2}?)[\\\.(\d+)]{0,}',x)

I am not able to understand how shall I write the regular expression. The values can be fetched in the form of a list also. I write few more patterns but it isn't helping.

2
  • If you're going to process a lot of strings you should compile your regex - it's a lot more efficient than creating the same regex over & over in a loop. Commented Jun 4, 2015 at 11:30
  • @PM2Ring thank you very much, it would be of great help :) Commented Jun 4, 2015 at 11:48

3 Answers 3

5

In regex, you may use re.findall function.

>>> import re
>>> s = 'GigabitEthernet0/0/0/0 '
>>> s[:2]+''.join(re.findall(r'\d', s))
'Gi0000'

OR

>>> ''.join(re.findall(r'^..|\d', s))
'Gi0000'
>>> ''.join(re.findall(r'^..|\d', 'Ethernet0/0.222'))
'Et00222'

OR

>>> s = 'GigabitEthernet0/0/0/0 '
>>> s[:2]+''.join([i for i in s if i.isdigit()])
'Gi0000'
Sign up to request clarification or add additional context in comments.

3 Comments

you can use s[:2] and a list comprehension within join for more performance
Now you just need to turn that last gen exp into a list comp. :) As Kasra said, you get better performance that way. That's because join has to scan its iterable arg twice - the 1st scan is to determine the size of the string it needs to allocate. So if you pass it a generator it has to build that into a list before it can join it.
Lots of people don't know that. :) So I'll leave that comment for the benefit of future readers, even though you've fixed it now.
0
z="Ethernet0/0.222."
print z[:2]+"".join(re.findall(r"(\d+)(?=[\d\W]*$)",z))

You can try this.This will make sure only digits from end come into play .

3 Comments

But from the OP it looks like Vipul wants all the digits, eg Ethernet0/0.222 -> Et00222
@PM2Ring Gig9abitEthernet0/0/0/2 i dont think OP would want 9 from this ex..He should clarify though
@Vipul what is the expected output for Gig9abitEthernet0/0/0/2 do you want 9 here?
0

Here is another option:

s = 'Ethernet0/0.222'

"".join(re.findall('^\w{2}|[\d]+', s))

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.