Reputation: 2981
Given the following type of input (contained within numerous files):
ret = myFunc(&pConfig->hConstraintList, &pConfig->util,
2, sizeof(rcConstraint_t),
NULL, NULL, NULL, NULL, NULL);
I'm trying to get the following regex to match:
myFunc\((\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*(\S*?),\s*NULL,\s*NULL,\s*NULL,\s*NULL\s*\).*;
The idea is to replace this with the following:
myFuncNoCB($1 $2 $3 $4 $5);
This works as a .NET regex, but doing in-file search and replace in powershell is a pain, and the VS UI doesn't fully support multiline expressions.
So...perl. But I can't even get the match to work:
Here's what I've tried:
perl -0ne '/myFunc\((\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*(\S*?),\s*NULL,\s*NULL,\s*NULL,\s*NULL\s*\).*;/gms' myfile.cpp
My assumption is that perl doesn't consider a newline whitespace even in multiline mode? That's going to be a complete nightmare as each parameter could have any number of spaces, tabs, newlines, in any order between them.
I'm interested in any solution that works, to be frank. There's a lot of variables here. I did a perl -pi -e 's/\r//g'
on this particular file to eliminate that potential issue but I would love a solution that doesn't choke on \r
as well.
PS--I've google searched this question, and searched on stackoverflow. The issue is that most people that use "multiline" just need to search multiple lines...one at a time. Those that use it correctly have significantly different patterns they are trying to match that don't help me. If there's an answer that helps me, I couldn't find it.
Upvotes: 1
Views: 314
Reputation: 241858
You don't need the /m
modifier, as it changes the behaviour of ^
and $
none of which you use in the regex.
The same applies to /s
- it makes .
match a newline, but you don't need it, \s
already matches all whitespace including newlines.
I'd list commas in the regex and omit the .*
at the end as it could (with /s
) match the rest of the document.
perl -0pe 's/myFunc\((\S*),\s*(\S*),\s*(\S*),\s*(\S*),\s*(\S*?),
\s*NULL,\s*NULL,\s*NULL,\s*NULL\s*\)
/myFuncNoCB($1, $2, $3, $4)/xg' -- file
Upvotes: 3