-3

I am trying a couple of regex´s to try to extract a txt from a file.

I try with this example with no sucess

lets say I have this string:

This is _localizer["my test 1."]' (here I want 'my test 1.')

or

This is _localizer["my test 2."] And this is _localizer["my test 3."]' (here I want 'my test 2.' and 'my test 3.')

In other others, the regex need to understand the begin anchor (_localizer[") and the end anchor ("]) and need to know that sometime there are multiple 'localizer' in the same line.

In the txt I want to extract sometimes will have / or \ and others characters that may will make regex fail.

What I am trying to do:

Regex r = new Regex(@"_localizer[(.+?)]");
                var matches = r.Matches(txt)
                                .Select(xx=> xx.Value)
                                .Distinct();

cheers

Maciej Los
  • 8,468
  • 1
  • 20
  • 35
2Fast4YouBR
  • 1,012
  • 1
  • 12
  • 22

1 Answers1

0

your regex string should look something like this:

new Regex(@"(?:_localizer\[""(.+?)""\])");

Let's break that down.

first there is (?: - this tells us that we require is inside the parenthesis for a match, but we don't want the data in our output

then _localizer\["" - this is just a literal match

next (.+?) - the . matches any character, the + tells us we want one or more, and the ? makes the + lazy, meaning it will match only the least amount of characters it can. by surrounding this statement with () we at saying that this data is what we want

finally ""\]) - a literal for the closing brackets, and closing the starting parenthesis.

also to get that data into an easy list you will want to change your LINQ to:

var matches = r.Matches(txt)
               .Select(xx=> xx.Groups[1].Value)
               .Distinct();
Whistler
  • 174
  • 6
  • Worked OK... but when there are 2 localizers in the same line, this regex only captures the first one. And its returning the txt and the anchors together – 2Fast4YouBR Jul 27 '20 at 13:55
  • Did you make changes to the Select Statement? the c# regex engine is weird in that it returns everything in the first group, so `Groups[1]` has the data you want. As for the multiple matches per line, i'll need to see more of your code, because the test program I wrote had no issues with that – Whistler Jul 27 '20 at 14:17
  • its working fine... but when the end anchor is in another line, it´s not working... like only the ] will be in another line... Chnaged the regex options to SingleLine but did not work – 2Fast4YouBR Jul 31 '20 at 19:40
  • https://dotnetfiddle.net/oyDea6 is that what you tried? – Whistler Aug 06 '20 at 13:55