can we use regular expression in split or strip function??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • techyvibe
    New Member
    • Feb 2008
    • 5

    can we use regular expression in split or strip function??

    can we use regular expression in split or strip function??

    i have a file as below

    abc started on port 3458
    status abc 255.255.255.345 :5679 closed

    i need to strip or split the above file wherever there are numericals in it ,as the port number keeps changing on running the scripts and it has to be compared with a sample file each time

    is there any better way to achieve the same ??
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by techyvibe
    can we use regular expression in split or strip function??

    i have a file as below

    abc started on port 3458
    status abc 255.255.255.345 :5679 closed

    i need to strip or split the above file wherever there are numericals in it ,as the port number keeps changing on running the scripts and it has to be compared with a sample file each time

    is there any better way to achieve the same ??
    You only want the port number? Maybe one of the following will work for you.[code=Python]lineList = ['abc started on port 3458', 'status abc 255.255.255.345 :5679 closed']

    import re

    patt = re.compile(r'po rt (\d+)')
    for item in lineList:
    m = patt.search(ite m)
    if m:
    print m.group(1)

    patt = re.compile(r'([0-9]+)$')
    for item in lineList:
    for word in item.split():
    m = patt.match(word )
    if m:
    print m.group(1)

    for item in lineList:
    if 'port' in item:
    print item.split()[-1][/code]

    Comment

    • techyvibe
      New Member
      • Feb 2008
      • 5

      #3
      thanks :) ...i actually want to eliminate the port no. and have only the text either before or after the port number


      Originally posted by bvdet
      You only want the port number? Maybe one of the following will work for you.[code=Python]lineList = ['abc started on port 3458', 'status abc 255.255.255.345 :5679 closed']

      import re

      patt = re.compile(r'po rt (\d+)')
      for item in lineList:
      m = patt.search(ite m)
      if m:
      print m.group(1)

      patt = re.compile(r'([0-9]+)$')
      for item in lineList:
      for word in item.split():
      m = patt.match(word )
      if m:
      print m.group(1)

      for item in lineList:
      if 'port' in item:
      print item.split()[-1][/code]

      Comment

      Working...