Kamil Kisiel
Kamil Kisiel

Reputation: 20451

Regex replacement character only if group matches

In a sed/egrep-style regular expression is it possible to print a character in the replacement string only if one of the groups matched?

For example, suppose I have an expression such as:

/^func([ \t]+\([^)]+\))?[ \t]+([a-zA-Z0-9_]+)/\1.\2/

Is it possible to print the period in the replacement only if the group \1 matched?

Specifically I'm trying to write an expression for the --regex-<LANG> option as described in http://ctags.sourceforge.net/ctags.html

Upvotes: 2

Views: 752

Answers (1)

krlmlr
krlmlr

Reputation: 25444

The only thing I can think of is two replace commands:

/^func[ \t]+([a-zA-Z0-9_]+)/\1/
/^func([ \t]+\([^)]+\))?[ \t]+([a-zA-Z0-9_]+)/\1.\2/

The documentation of ctags suggests that this is supported by simply specifying two --regex-<LANG> options:

The regular expression defined by this option is added to the current list of regular expressions for the specified language unless the parameter is omitted, in which case the current list is cleared.

In Perl, you can call arbitrary function on the group matches, but this doesn't help here.

Upvotes: 2

Related Questions