Phil-ZXX
Phil-ZXX

Reputation: 3245

Find regex matches & remove outer part of the match

I have a string

content = "std::cout << func(some_val) << std::endl; auto i = func(some_other_val);"

and I find to find all instances with func(...), and remove the function call. So that I would get

content = "std::cout << some_val << std::endl; auto i = some_other_val;"

So I've tried this:

import re
content = "std::cout << func(some_val) << std::endl; auto i = func(some_other_val);"
c       = re.compile('func\([a-zA-Z0-9_]+\)')

print(c.sub('', content))           # gives "std::cout <<  << std::endl; auto i = ;"

but this removes the entire match, not just the func( and ).

Basically, how do I keep whatever matched with [a-zA-Z0-9_]+?

Upvotes: 2

Views: 46

Answers (2)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

You can use re.sub to replace all the outer func(...) with only the value like below, See regex here , Here I've used [w]+, you can do changes if you use

import re
regex = r"func\(([\w]+)\)"

test_str = "std::cout << func(some_val) << std::endl; auto i = func(some_other_val);"
subst = "\\1"
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
    print (result)

Demo: https://rextester.com/QZJLF65281

Output:

std::cout << some_val << std::endl; auto i = some_other_val;

Upvotes: 1

Sweeper
Sweeper

Reputation: 270860

You should capture the part of the match that you want to keep into a group:

re.compile(r'func\(([a-zA-Z0-9_]+)\)')

Here I captured it into group 1.

And then you can refer to group 1 with \1:

print(c.sub(r'\1', content))

Note that in general, you should not use regex to parse source code of a non-regular language (such as C in this case) with regex. It might work in a few very specific cases, where the input is very limited, but you should still use a C parser to parse C code. I have found libraries such as this and this.

Upvotes: 1

Related Questions