Ryan Drake
Ryan Drake

Reputation: 643

Regex help: Targeting text between *| and |*

I'd love someone to help me with this regex. I'd like to be able to isolate the regex to detect characters in the string below between the * | and | *:

Hello this is "*|first_name||default_value|*"

I appreciate your help.

Upvotes: 0

Views: 48

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110685

Nothing fancy:

'Hello this is "*|first_name||default_value|*"'[/\*\|(.+)?\|\*/,1]
  #=> "first_name||default_value"

Upvotes: 0

zx81
zx81

Reputation: 41838

Use this:

if subject =~ /\*\|\K.*?(?=\|\*)/
    match = $&

See demo.

  • \*\| matches *|
  • \K tells the engine to drop what was matched so far from the match it will report
  • .*? lazily matches everything up to...
  • a point where the lookahead (?=\|\*) can assert that what follows is |*

Reference

Upvotes: 4

Related Questions