Complex regex in Python

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 ='([\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.


In regex, you may use re.findall function.

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


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


>>> s = 'GigabitEthernet0/0/0/0 '
>>> s[:2]+''.join([i for i in s if i.isdigit()])

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 .

Category:python Time:2018-10-07 Views:0
Tags: regex python

Related post

  • How to use regex in python in getting a string between two characters? 2011-06-12

    I have this as my input content = ''\n I want to take out abc out of it . How do I do it using regex in python ? Edit : No this is not a homework question . I am trying to automate something and I am stuck at a certain point so that I can make

  • HTML tag replacement using regex and python 2009-09-27

    I have a Python script that will look at an HTML file that has the following format: <DOC> <HTML> ... </HTML> </DOC> <DOC> <HTML> ... </HTML> </DOC> How do I remove all HTML tags (replace the tags with

  • Is_prime function via regex in python (from perl) 2010-02-08

    I've read this article where the /^1?$|^(11+?)\1+$/ Perl regex is used to test if a number is prime or not. Process: s = '1' * your_number If s matchs the regex, then it's not prime. If it doesn't, it's prime. How would you translate that regex to Py

  • Difference in regex between Python and Rubular? 2010-05-09

    In Rubular, I have created a regular expression: (Prerequisite|Recommended): (\w|-| )* It matches the bolded: Recommended: good comfort level with computers and some of the arts. Summer. 2 credits. Prerequisite: pre-freshman standing or permission of

  • Ruby Regex vs Python Regex 2011-04-15

    Are there any real differences between Ruby regex and Python regex? I've been unable to find any differences in the two, but may have missed something. --------------Solutions------------- The last time I checked, they differed substantially in their

  • Convert AWK regex to Python script 2011-05-16

    Good morning all, Wondering if you could please help me with the following query:- I have just started learning Python last weekend after a colleague of mine showed me how to dramatically cut the time a Bash script takes to execute by re-writing it i

  • Regex in Python to find words that follow pattern: vowel, consonant, vowel, consonant 2011-05-21

    Trying to learn Regex in Python to find words that have consecutive vowel-consonant or consonant-vowel combinations. How would I do this in regex? If it can't be done in Regex, is there an efficient way of doing this in Python? --------------Solution

  • Access std::complex from gdb python macro 2011-07-19

    I'm trying to write a python script that evaluates a vector Complex numbers so I can do things like plot it via matplotlib. Python code below with comment where code breaks: import sys import gdb import matplotlib.pyplot as plt class Plotter (gdb.Com

  • Complex regex to filter out numbers from string, but with exceptions 2012-02-11

    I need a fairly complex regex to accomplish the following: > replace numbers in a string, i.e. 700, 12.43 by a label (format: {NUMBER:xx}) > ignore: when number is between {braces}, i.e. {7}, {7th} > ignore: when any character is attached to

  • Generalised text extraction from web pages using regex and python 2012-03-03

    I need to extract text from any kind of web page using regex in python. My code works fine with html tags but due to irregular syntax of tags and attributes enclosed between script tags, the code I came up with extracts some scripting data in additio

  • Should I create one complex RegEx or multiple and less complex ones? 2012-03-16

    Should I create one complex RegEx to tackle all cases on hand or should I break one complex RegEx in multiple Regex which ? I'm concerned regarding performance using complex Regex. Will breaking the complex Regex into smaller simple regex perform bet

  • sed regex to Python re 2012-04-27

    Can anyone help me with translating this regex to Python re module? sed -e "s/^[^ ]* \([^ ]*\) \([0-9]*\) \([0-9:]*\) \([0-9]*\) /\1 \2 \4 \3 /" As I've understood this replaces one text to another. How can I create something like this using re modul

  • convert vim regex to python for re.sub 2015-02-04

    I have a working regex under vim: /^ \{-}\a.*$\n I implement a global search and replace as :%s/^ \{-}\a.*$\n// This works great -- removes all lines that start with any number of spaces (matched non-greedily), followed by a letter and anything else

  • Python regex for Python function signatures 2012-04-14

    I try to write a python regex, which matches the signature of simple python functions. Like: def _func1_(arg1, arg2): I created this regex: "def ([^\W\d]+\w*)(\(( *[^\W\d]+\w* *,? *)*\)):" Unfortunately, this is not really good. In the argument list,

  • Function Parser with RegEx in Python 2009-03-12

    I have a source code in Fortran (almost irrelevant) and I want to parse the function names and arguments. eg using (\w+)\([^\(\)]+\) with a(b(1 + 2 * 2), c(3,4)) I get the following: (as expected) b, 1 + 2 * 2 c, 3,4 where I would need a, b(1 + 2 * 2

  • My regex in python isn't recursing properly 2009-06-05

    I'm suppose to capture everything inside a tag and the next lines after it, but it's suppose to stop the next time it meets a bracket. What am i doing wrong? import re #regex regex = re.compile(r""" ^ # Must start in a newline first \[\b(.*)\b\] # Ge

  • jquery match() variable interpolation - complex regexes 2009-12-24

    I've already looked at this, which was helpful to a point. Here's the problem. I have a list of users propagated into an element via user click; something like this: <div id="box"> joe-user page-joe-user someone-else page-someone-else </div

  • Determining the unmatched portion of a string using a regex in Python 2010-02-03

    Suppose I have a string "a foobar" and I use "^a\s*" to match "a ". Is there a way to easily get "foobar" returned? (What was NOT matched) I want to use a regex to look for a command word and also use the regex to remove the command word from the str

  • How do I do this regex in Python? 2010-03-09

    Suppose I have a string of text, of all characters Latin-based. With punctuation. How do I "find" all the characters and put <strong> tags around it? hay = The fox jumped up the tree. needle = "umpe" In this case, part of the word "jumped" woul

Copyright (C), All Rights Reserved.

processed in 0.675 (s). 13 q(s)