Anon Dev
Anon Dev

Reputation: 1411

Regex: Pattern to match any text followed by specific enclosing format

I'm trying to find the way to get data from an URL using Regex, the URL is build like this:

https://myurl.com/action?AAA={10/17/2018 08:00:00}&BBB={XXX123}&CCC={DATA_DATA}&DDD={29696}&EEE={10/17/2018 08:00:00}&FFF={CCC}&GGG={SOMEINFO}&HHH={7234384}&III={https://www.otherurl.com/Page?utm_source=email&utm_medium=medium}

Basically what I expect to get is a List of strings like this:

AAA={10/17/2018 08:00:00}&
BBB={XXX123}&
CCC={DATA_DATA}&
DDD={29696}&
EEE={10/17/2018 08:00:00}&
FFF={CCC}&
GGG={SOMEINFO}&
HHH={7234384}&
III={https://www.otherurl.com/Page?utm_source=email&utm_medium=medium}

By code I'm removing the first URL part (https://myurl.com/action?) so I can use something like Regex.Split to generate the final list, But can't find an easy way to match the pattern, it should be something like:

anything={anything}

I've tried using ".={.}" but didn't work, also @"/\w+={[]}" without any luck

Upvotes: 3

Views: 54

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627103

You may get these matches using

(\w+)={([^{}]*)}

See the regex demo.

Details

  • (\w+) - Group 1: one or more word chars
  • ={ - a ={ substring
  • ([^{}]*) - Group 2: any 0+ chars other than { and }
  • } - a } char.

C# code example:

var s = "https://myurl.com/action?\nAAA={10/17/2018 08:00:00}&\nBBB={XXX123}&\nCCC={DATA_DATA}&\nDDD={29696}&\nEEE={10/17/2018 08:00:00}&\nFFF={CCC}&\nGGG={SOMEINFO}&\nHHH={7234384}&\nIII={https://www.otherurl.com/Page?utm_source=email&utm_medium=medium}";
var res = Regex.Matches(s, @"(\w+)={([^{}]*)}")
        .Cast<Match>()
        .ToDictionary(
            m => m.Groups[1].Value,
            m => m.Groups[2].Value);
foreach (var kvp in res)
    Console.WriteLine("{0} => {1}", kvp.Key, kvp.Value);

Output:

AAA => 10/17/2018 08:00:00
BBB => XXX123
CCC => DATA_DATA
DDD => 29696
EEE => 10/17/2018 08:00:00
FFF => CCC
GGG => SOMEINFO
HHH => 7234384
III => https://www.otherurl.com/Page?utm_source=email&utm_medium=medium

Upvotes: 4

Related Questions