Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing pipe symbol delimited digits with regex

Tags:

regex

php

I need to capture the digits (ids) from a "|" delimited string, This is an example string:

|98|34|2|{"flag":"CHART"}

This is the expression that seem to work (for the first digit)

^\|(\d+)\|

The problem is that this only captures the first digit.

I don't know how to make this regex repeat itself till it reaches the "{" symbol. Help would be appreciated!

And please tell me if there is a more efficient way to do this (in php)

like image 426
Buster Avatar asked Nov 27 '25 17:11

Buster


1 Answers

Use the \G anchor to start the next match on the same string where the last match left off.

(?:^\||\G)\|?(\d+)(?=\|)

DEMO

like image 176
Avinash Raj Avatar answered Nov 29 '25 07:11

Avinash Raj



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!