Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match string using regex which includes vertical bar

Tags:

regex

I have:

|123|456|789|

I can capture only |123| using regex

\|(\d*)\|

But not sure how to capture the full string. I am quite new to this.

I will be thankful for any help

like image 956
black Avatar asked Sep 04 '26 13:09

black


2 Answers

^\|(\d*\|?)*

This should work. Start with vertical bar and repeat numbers and optional vertical bar.

like image 115
James Avatar answered Sep 07 '26 11:09

James


You can do:

^\|[\d|]*
  • ^\| matches literal | at start, as | is a Regex token we need to escape it

  • [\d|]* matches any number of digits or |, | inside [] is treated literally

Demo


From comment, if there must be a | at the end, do:

^\|[\d|]*\|$
like image 30
heemayl Avatar answered Sep 07 '26 11:09

heemayl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!