2

Got some great help from this post answer a while back here

Now I'm trying take that and capture a couple patterns in my multi-line string but not working. Any hints for me?

Here's a .net fiddle I'm playing with

    desired result:
    //these can be letters
      
    1: 3333  
    2: 1111 

code:

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        var item = " john smith (ABCDEFG) <js@email.com>\n" + 
            "target1 = 3333, j\n" + 
            "target2 1111, ";
        String[] patternArr = {"(?:\\s*)", 
                               "(?<target1>target1 = (\\w+)])", // capture the first target
                                "(?:\\s*)", 
                               "(?<target2>target2 (\\w+))", // captures the second target
 };
        var pattern = String.Join("", patternArr);
        var m = Regex.Match(item, pattern);
        if (m.Success)
        {
            Console.WriteLine("target1: {0}", m.Groups["target1"]);
            Console.WriteLine("target2: {0}", m.Groups["target2"]);
        }
    }
}
Rod
  • 14,529
  • 31
  • 118
  • 230
  • Note `\t` in `"(?\target2 (\\w+))"` is a TAB, I think you wanted `t`. The main issue is that your regex just does not match the string like that. What are the pattern requirements? Try `var pattern = @"target1 = (?.+)\ntarget2(?.+)";`, maybe it will solve the issue. – Wiktor Stribiżew Oct 02 '21 at 23:11
  • ok, just updated OP. All I need are the 4-alphanumeric characters – Rod Oct 02 '21 at 23:25

1 Answers1

1

You can use

var pattern = @"target1 = (?<target1>\w+).*\ntarget2\s+(?<target2>\w+)";

See the regex demo. Regex details:

  • target1 = - a literal string
  • (?<target1>\w+) - Group "target1": one or more word chars
  • .* - the rest of the line
  • \n - the newline char
  • target2 - literal string
  • \s+ - one or more whitespaces
  • (?<target2>\w+) - Group "target2": one or more word chars

See the C# demo:

using System;
using System.Text.RegularExpressions;
 
public class Program
{
    public static void Main()
    {
        var item = " john smith (ABCDEFG) <js@email.com>\n" + 
            "target1 = 3333, j\n" + 
            "target2 1111, ";
        var pattern = @"target1 = (?<target1>\w+).*\ntarget2\s+(?<target2>\w+)";
        var m = Regex.Match(item, pattern);
        if (m.Success)
        {
            Console.WriteLine("target1: {0}", m.Groups["target1"]);
            Console.WriteLine("target2: {0}", m.Groups["target2"]);
        }
    }
}

Output:

target1: 3333
target2: 1111
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Also need `RegexOptions.Singeline` and remove the `\n`, that way it could be on more lines than two – Charlieface Oct 02 '21 at 23:54
  • @Charlieface If it were, I would mention. The lines are consecutive in the original input. – Wiktor Stribiżew Oct 02 '21 at 23:55
  • I noticed if I swap targets in the item value the match doesn't occur. Can you please help me understand how the order is important? What if I didn't know which one comes first? https://dotnetfiddle.net/7RGV6a – Rod Oct 03 '21 at 17:10
  • @Rod It is evident, isn't it? Look, if you have a pattern like `a b` and a string `b a`, there is just no match. The `a b` pattern matches only `a b`. A possible solution will look like [this](https://ideone.com/RzaNpd). – Wiktor Stribiżew Oct 03 '21 at 18:31
  • @WiktorStribiżew is it suppose to work here at regex101 https://regex101.com/r/qIyYCV/1 – Rod Oct 04 '21 at 01:56
  • 1
    @Rod Sure, just give it the *plain text*: https://regex101.com/r/qIyYCV/2 – Wiktor Stribiżew Oct 04 '21 at 07:06
  • @WiktorStribiżew I'm not sure I understand why my version didn't work, would you mind sharing more insight? sorry. – Rod Oct 04 '21 at 13:40
  • 1
    @Rod you are using a string literal at regex101.com, while you should be using *plain text*. Please familiarize with [this post](https://stackoverflow.com/a/39636208/3832970). – Wiktor Stribiżew Oct 04 '21 at 13:42