#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    9

    Simple regular expression question


    I have names like this:

    Dsim\GD15104-RA

    I would like to replace the '\' with '_'.

    I can see two approaches to this, but I can make neither of them work and I would like to understand how I can make each of them work:

    1. I could try to make a pattern that is '\':

    Code:
    name = 'Dsim\GD15104-RA'
    pattern = r'\'
    regexp = re.compile(pattern)
    name = regexp.sub('_', name)
    This doesn't work, and I think the problem is that the second ' in pattern = r'\' is viewed as an escaped ', so it's like saying:

    Code:
    pattern = r'\
    with an unmatched '. But I don't know how to do this correctly.

    2. I could also try something like this:

    Code:
    name = 'Dsim\GD15104-RA'
    pattern = r'^(?P<first>Dsim)\(?P<second>GD\S+$)'
    regexp = re.compile(pattern)
    result = regexp.search(name)
    if result:
        name = result.group('first') + '_' + result2.group('second')
    else:
        sys.exit('problem with name: ' + name)
    But this doesn't work either. I think it's again a problem with the escapes and the second ( in pattern = ... being viewed as an escaped (, so there's an unmatched ). But again, I don't know how to do this correctly.

    If anyone could show me how to do each of these things correctly, I would very much appreciate it.

    Thank you.

    Eric
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    use string replace method.
    Code:
    >>> A=r'Dsim\GD15104-RA'
    >>> print(A)
    Dsim\GD15104-RA
    >>> dir(A)
    xxx interesting stuff removed
    >>> A.replace('\\','_')
    'Dsim_GD15104-RA'
    >>>
    And as I was recently told, \' is "'" even in r'string'. Or something like that.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    9
    Originally Posted by b49P23TIvg
    use string replace method.
    Code:
    >>> A=r'Dsim\GD15104-RA'
    >>> print(A)
    Dsim\GD15104-RA
    >>> dir(A)
    xxx interesting stuff removed
    >>> A.replace('\\','_')
    'Dsim_GD15104-RA'
    >>>
    And as I was recently told, \' is "'" even in r'string'. Or something like that.
    Perfect. Thanks so much.

    Best wishes,

    Eric
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    605
    Rep Power
    65
    Another look at this problem:
    Code:
    ''' backslash_sub1.py
    replace the backslash symbol with another character
    use chr(92) to avoid problems, 92 is the ASCII value of \
    '''
    
    name = r'Dsim\GD15104-RA'
    name2 = "".join(c if c != chr(92) else '_' for c in name)
    print(name)
    print(name2)
    
    ''' result -->
    Dsim\GD15104-RA
    Dsim_GD15104-RA
    '''
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    54
    Rep Power
    9
    Originally Posted by Dietrich
    Another look at this problem:
    Code:
    ''' backslash_sub1.py
    replace the backslash symbol with another character
    use chr(92) to avoid problems, 92 is the ASCII value of \
    '''
    
    name = r'Dsim\GD15104-RA'
    name2 = "".join(c if c != chr(92) else '_' for c in name)
    print(name)
    print(name2)
    
    ''' result -->
    Dsim\GD15104-RA
    Dsim_GD15104-RA
    '''
    Thanks, Dietrich! That's a very interesting solution to the problem.

    Best wishes,

    Eric

IMN logo majestic logo threadwatch logo seochat tools logo