hardywang
hardywang

Reputation: 5192

Regular expression match any string within a quote or null

For example I have a file with multiple lines, some of the lines have a string "SocialInsuranceNumber": "123456789", while some may have SocialInsuranceNumber": null,. I need to find all such lines by using regular expression. And eventually replace the value after :.

Any suggestion how to write a regex in this case?

Upvotes: 0

Views: 47

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627488

You may use

"SocialInsuranceNumber"\s*:\s*(?:"\d+"|null),

See the regex demo.

The pattern matches:

  • "SocialInsuranceNumber" - a literal char sequence
  • \s*:\s* - a hyphen enclosed with 0+ whitespaces (\s*)
  • (?:"\d+"|null) - a non-capturing with two alternatives:
    • "\d+" - ", 1+ digits, "
    • | - or
    • null - a sequence of literal chars
  • , - a literal comma

Upvotes: 1

Related Questions