2

I have these examples:

{I18n.get("Testing 123...")}

{I18n.get('Testing 123...')}

    {I18n.get(
            "Testing 123..."
     )}
    
    {I18n.get("Testing 123..."
     )}
    
    {I18n.get(
            "Testing 123...")}
        
        

I want to extract the 'Testing 123...' in .Net using C# Regex. What I did was:

        Regex r = new Regex(@"(?:I18n.get\(""(.+?)""\))", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        var matches = r.Matches(txt)
                        .Select(xx=> xx.Groups)
                        .Select(xx=> xx.Last().Value)
                        .ToList(); 

When is single line works perfect, but when is multiple line it fails...

And how would be possible to match in a single Regex when the txt is with double quotes " or when is with single quotes ' ?

4
  • 2
    You need to match whitespace, @"I18n\.get\(\s*""(.+?)""\)". Also, you do not need two Select, just use r.Matches(txt).Cast<Match>().Select(x=>x.Groups[1].Value).ToList() Commented Jul 31, 2020 at 19:50
  • This way it grabs more than have to.. looks like is not finding the end )... Commented Jul 31, 2020 at 19:58
  • Ok, add \s* on the right, @"I18n\.get\(\s*""(.*?)""\s*\)", see demo. Commented Jul 31, 2020 at 20:00
  • Perfect! did a .Net exemple but you answered first dotnetfiddle.net/glGGvF . How about capture when its in Double Quotes and when is in Single Quotes.. would be possible in one regex? Make an answer so I can accept. cheers Commented Jul 31, 2020 at 20:09

2 Answers 2

1

You may use

var r = new Regex(@"I18n\.get\(\s*(""|')(.*?)\1\s*\)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
var results = r.Matches(txt).Cast<Match>().Select(x => x.Groups[2].Value).ToList();

See the regex demo.

Details

  • I18n\.get\( - a literal I18n.get( text
  • \s* - 0+ whitespaces
  • ("|') - Group 1: " or '
  • (.*?) - Group 2: any char, 0 or more occurrences, as few as possible
  • \1 - same value as captured in Group 1
  • \s* - 0+ whitespaces
  • \) - a ) char.
Sign up to request clarification or add additional context in comments.

2 Comments

Not working Wiktor... dotnetfiddle.net/2WAFVX
@2Fast4YouBR Because you are not using my code. The value you need is in Group 2: Console.WriteLine(m.Groups[2].Value);
0

I contend it doesn't matter which open/close quotes should be in the regex when you don't intend to actually parse it as a quoted string, right ?
I mean with all the embed escapes etc...

Use what you know as the delimiters, the text literals
I18n.get( stuff here )

You could use a sub validation that there's an inner quote, but since you're
not parsing quotes, it's not going to be strictly valid anyway.
Here, we just use it to trim so it's not matched and be part of element.

Here it is, the whole match is the value you're looking for, toss it into an array.

@"(?s)(?<=I18n\s*\.\s*get\s*\(\s*['""]\s*).*?(?=\s*['""]\s*\))"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.