Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Operation on captured group in re Python

Tags:

python

regex

I have a string:

str1 = "abc = def"

I want to convert it to:

str2 = "abc = #Abc#"

I am trying this:

re.sub("(\w+) = (\w+)",r"\1 = %s" % ("#"+str(r"\1").title()+"#"),str1)

but it returns: (without the string operation done)

"abc = #abc#"
  • What is the possible reason .title() is not working.?
  • How to use string operation on the captured group in python?
like image 995
Jay Joshi Avatar asked Jun 23 '18 07:06

Jay Joshi


People also ask

What is capturing group in regex Python?

Capturing groups are a handy feature of regular expression matching that allows us to query the Match object to find out the part of the string that matched against a particular part of the regular expression. Anything you have in parentheses () will be a capture group.

What is group () in Python?

The re. groups() methodThis method returns a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None.

How do capture groups work regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .

Does re search return a string?

While re. findall() returns matches of a substring found in a text, re. match() searches only from the beginning of a string and returns match object if found. However, if a match is found somewhere in the middle of the string, it returns none.


2 Answers

You can see what's going on with the help of a little function:

import re

str1 = "abc = def"

def fun(m):
    print("In fun(): " + m)
    return m

str2 = re.sub(r"(\w+) = (\w+)",
    r"\1 = %s" % ("#" + fun(r"\1") + "#"),
    #                   ^^^^^^^^^^
    str1)

Which yields

In fun(): \1

So what you are basically trying to do is to change \1 (not the substitute!) to an uppercase version which obviously remains \1 literally. The \1 is replaced only later with the captured content than your call to str.title().

Go with a lambda function as proposed by @Rakesh.

like image 165
Jan Avatar answered Sep 19 '22 07:09

Jan


Try using lambda.

Ex:

import re
str1 = "abc = def"
print( re.sub("(?P<one>(\w+)) = (\w+)",lambda match: r'{0} = #{1}#'.format(match.group('one'), match.group('one').title()), str1) )

Output:

abc = #Abc#
like image 20
Rakesh Avatar answered Sep 20 '22 07:09

Rakesh