Home > Net >  Regex to match a pattern and extract field after :
Regex to match a pattern and extract field after :

Time:11-04

I'm trying to write a regex to match the value of a column based on a pattern.

Text = {"key1":value1,"key2":value2,"366-Pat?":Complete}
i.e, (?i)"[^"]* Pat\b[^"] -> matches "366-Pat?" 

I'm trying to get the value "Complete"

**Expected result**: Complete

CodePudding user response:

You are missing a few characters to match until you get to Complete, and there is a - before Pat in the example string.

You could use a capture group and allow/disallow the characters for the value that you want:

(?i)"[^"]*\bPat\b[^"]*":([^"{}] )
  • (?i) Inline modifier for a case insensitive match
  • "[^"]* Match " and then optionally any char except "
  • \bPat\b Match the word Pat between word boundaries
  • [^"]*" Optionally match any char except " and then match the closing "
  • (:[^"{}] ) Capture group 1, match : followed by any char except what should not be allowed

Regex demo

  • Related