How do I match this with re

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sunit Joshi

    How do I match this with re

    Hello All
    I have a problem here where I need to find the drive letters in a text
    file and replace them with another letter. Now the lines can be as
    long as this

    6 'C:\pds\bddin64 \report\mto\' 'SJOSHI'
    'D:\pds\bddin64 \report\format\ ' 'SJOSHI'

    with multiple entries for path; i.e. there may be 2, as above or 1 or
    more then 2. If I use the expression .*'([A-Z]:).*?', it only matches
    the 2nd D: How do I get all the path groups ..??


    thanks
    Sunit
  • Terry Reedy

    #2
    Re: How do I match this with re


    "Sunit Joshi" <[email protected] m> wrote in message
    news:8f8ffe67.0 309040835.368f9 [email protected] gle.com...[color=blue]
    > Hello All
    > I have a problem here where I need to find the drive letters in a[/color]
    text[color=blue]
    > file and replace them with another letter. Now the lines can be as
    > long as this
    >
    > 6 'C:\pds\bddin64 \report\mto\' 'SJOSHI'
    > 'D:\pds\bddin64 \report\format\ ' 'SJOSHI'
    >
    > with multiple entries for path; i.e. there may be 2, as above or 1[/color]
    or[color=blue]
    > more then 2. If I use the expression .*'([A-Z]:).*?', it only[/color]
    matches[color=blue]
    > the 2nd D: How do I get all the path groups ..??[/color]

    Use file.readline() or .readlines() and then process one line at a
    time.

    Terry


    Comment

    • Christos TZOTZIOY Georgiou

      #3
      Re: How do I match this with re

      On 4 Sep 2003 09:35:18 -0700, rumours say that [email protected] (Sunit
      Joshi) might have written:
      [color=blue]
      >Hello All
      >I have a problem here where I need to find the drive letters in a text
      >file and replace them with another letter. Now the lines can be as
      >long as this
      >
      >6 'C:\pds\bddin64 \report\mto\' 'SJOSHI'
      >'D:\pds\bddin6 4\report\format \' 'SJOSHI'
      >
      >with multiple entries for path; i.e. there may be 2, as above or 1 or
      >more then 2. If I use the expression .*'([A-Z]:).*?', it only matches
      >the 2nd D: How do I get all the path groups ..??[/color]

      I assume that the drive letters will be directly after a single quote.
      You need this re:

      reDriveLetter = re.compile(r"(? <=')[A-Z](?=:\\)").

      You can try reDriveLetter.f indall() or the reDriveLetter.f inditer()
      functions then, each match object will match a single capital letter.

      Very important to everyone working with regular expressions: the
      redemo.py script. Search for a Scripts directory in your python
      directories (it's under Tools on Windows, otherwise get it from the
      source distribution), redemo.py is in there along with other useful
      scripts.
      --
      TZOTZIOY, I speak England very best,
      Microsoft Security Alert: the Matrix began as open source.

      Comment

      Working...