Making letters uppercase using re.sub in python?

后端 未结 5 886
死守一世寂寞
死守一世寂寞 2020-11-30 07:26

In many programming languages, the following

find foo([a-z]+)bar and replace with GOO\\U\\1GAR

will result in the entire match bei

相关标签:
5条回答
  • 2020-11-30 07:47

    You can pass a function to re.sub() that will allow you to do this, here is an example:

     def upper_repl(match):
         return 'GOO' + match.group(1).upper() + 'GAR'
    

    And an example of using it:

     >>> re.sub(r'foo([a-z]+)bar', upper_repl, 'foobazbar')
     'GOOBAZGAR'
    
    0 讨论(0)
  • 2020-11-30 07:48

    Do you mean something like this?

    >>>x = "foo spam bar"
    >>>re.sub(r'foo ([a-z]+) bar', lambda match: r'foo {} bar'.format(match.group(1).upper()), x)
    'foo SPAM bar'
    

    For reference, here's the docstring of re.sub (emphasis mine).

    Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.

    0 讨论(0)
  • 2020-11-30 07:50

    You could use some variation of this:

    s = 'foohellobar'
    def replfunc(m):
         return m.groups()[0]+m.groups()[1].upper()+m.groups()[2]
    re.sub('(foo)([a-z]+)(bar)',replfunc,s)
    

    gives the output:

    'fooHELLObar'
    
    0 讨论(0)
  • 2020-11-30 08:02

    If you already have a replacement string (template), you may not be keen on swapping it out with the verbosity of m.group(1)+...+m.group(2)+...+m.group(3)... Sometimes it's nice to have a tidy little string.

    You can use the MatchObject's expand() function to evaluate a template for the match in the same manner as sub(), allowing you to retain as much of your original template as possible. You can use upper on the relevant pieces.

    re.sub(r'foo([a-z]+)bar', lambda m: 'GOO' + m.expand('\1GAR').upper())
    

    While this would not be particularly useful in the example above, and while it does not aid with complex circumstances, it may be more convenient for longer expressions with a greater number of captured groups, such as a MAC address censoring regex, where you just want to ensure the full replacement is capitalized or not.

    0 讨论(0)
  • 2020-11-30 08:02

    For those coming across this on google...

    You can also use re.sub to match repeating patterns. For example, you can convert a string with spaces to camelCase:

    def to_camelcase(string):
      string = string[0].lower() + string[1:]  # lowercase first
      return re.sub(
        r'[\s]+(?P<first>[a-z])',              # match spaces followed by \w
        lambda m: m.group('first').upper(),    # get following \w and upper()
        string) 
    
    to_camelcase('String to convert')          # --> stringToConvert
    
    0 讨论(0)
提交回复
热议问题