Manish Saraf Bhardwaj
Manish Saraf Bhardwaj

Reputation: 1058

Regex to extract substring which contains PIPE (|)

String1: \txd\tbuba:pub|rub|spo-os|ix|beach|app|one|op|sono\tadT\tad\t

String2: \txd\tbuba:pub\tadT\tad\t

I have two sample strings and need common regex to extract pub|rub|spo|ix|beach|app|one|op|sono and pub

I tried buba:(\w(.)*(-os)?)\\t but not working.

Please assist.

Upvotes: 0

Views: 68

Answers (1)

The fourth bird
The fourth bird

Reputation: 163217

You could use

buba:(\w+(?:\|\w+(?:-os)?)*)\\t

Explanation

  • buba: Match the string buba:
  • ( Capture group 1
    • \w+ Match 1+ word chars
    • (?:\|\w+(?:-os)?)* Optionally repeat | and 1+ word chars with optional -os
  • ) Close group 1
  • \\t Match \t

Regex demo

The value is in capture group 1.


Or a broader variant

buba:(\w+(?:\|[\w-]+)*)

Regex demo

Upvotes: 1

Related Questions