Reputation: 1940
I'm trying to handle some cases of strings with the regex:
(.*note(?:'|")?\s*=>\s*)("|')?(.*?)\2(.*)
Strings:
There can be multiple spaces in start/end/in between. I need to capture "
(or '
), $note
, ,
or whatever is left after note_section. There can be #
in beginning if this line is a comment, so, I've included .*
in beginning.
Given regex is failing in case 3 as there is \2
as null.
Edit:
Requirement is that I'm reading a file, and replacing the value of note with some tag say NOTETAG
, and all other things around remain same, including inverted commas and spaces. For that,
e.g. note => "kamal" ,
will become note => "NOTETAG" ,
(notice we didnt ate ,
from last)
Upvotes: 0
Views: 52
Reputation: 385764
s{
\b
note
\s*
=>
\s*
\K
(?: (.*)
| '[^']*'
| "[^"]*"
)
}{
defined($1)
? $1 =~ s{\$note\b}{"NOTETAG"}gr
: '"NOTETAG"'
}exg;
Upvotes: 3
Reputation: 37367
Yuo could try (note\s*=>\s*(?:"|')?)[^'",]+
Explanation:
(...)
- capturing group
note
- match note
literally
\s*
- match zero or more of whitespaces
=>
- match =>
literally
(?:..)
- non-capturing group
"|'
- alternation: match either '
or "
?
- match preceding pattern zero or one time
[^'",]+
- negated character class - match one or more chraacters (due to +
operator) other than '
, "
, ,
As a replacement use \1NOTETAG
, where \1
means first capturing group
Upvotes: 1