Reputation: 9100
I'm struggling to extract data between quotation marks in the following text:
msgid: "something"
msgidd: "something"
msgid:"something"
msgidd:"something"
msgid: "something"
msgidd: "something"
With (?<=msgsid:.{1}?)(.+[^"]")
I get what follows after msgid:
but not always what's inside the quotation marks. My goal is to replace what's inside the quotation marks yet keep msgid:
intact. Any help would be appreciated.
Thanks.
Upvotes: 0
Views: 1011
Reputation: 75272
I heartily agree that you shouldn't use lookbehind if you don't have to, but that isn't what's causing your problem. This is the culprit: .{1}?
. You probably meant for it to match an optional sixth letter like the second d
in some of your inputs. That would be simply .?
, but most regex flavors will reject that because of the constant-width requirement.
What .{1}?
really means is match exactly one character, non-greedily, which is kinda pointless, but it's syntactically valid. I tested it in several flavors, half expecting them to reject it anyway, but they all matched .{1}?
as if it were just .
. As for .?
, there are a few flavors (like .NET and Java) that will match it as you intended, and there are workarounds you can use in the other flavors, but in most cases the best solution is simply not to use lookbehind.
Upvotes: 1
Reputation: 9039
Why do you need a lookbehind? Try this:
msgid:\s*"([^"]+)"
And test it here at Rubular or RegexHero.
Update:
For the replace, try the below and test it here
Regex:
(msgid:\s*")[^"]+(")
Replacement:
$1replacementstring$2
Source:
msgid: "something"
msgidd: "something"
msgid:"something"
msgidd:"something"
msgid: "something"
msgidd: "something"
Final String:
msgid: "replacementstring"
msgidd: "something"
msgid:"replacementstring"
msgidd:"something"
msgid: "replacementstring"
msgidd: "something"
Anyway, if this is a challenge that you need to use a lookbehind, then use the below only to match. But it would not make sense to use this in your replace.
(?<=msgid:)\s*"([^"]+)"
Upvotes: 1
Reputation: 24506
Using lookbehind makes it a lot more difficult as the matching allowed is much more limited. It's easier to do:
(msgid+:\s*)"(.*?)"
And do the replacement as $1"something-replacement"
Upvotes: 3