I have a lot of strings containing custom tags:
hello {RT|12} world {RT|465} this is {RT|13243}a string with{RT|13}examples
I need a regex to filter out all numbers of tags of the form {RT|xx} with xx being a number of any possible length. Since I’m not that good with regex, I’ve been trying the following:
preg_match("/{RT|.*}/", $string, $matches)
This is now filtering out the {RT|xx}-tags, but what I need for $matches to hold is an araay(12, 456, 13243, 13). Who can help me out?
@Qtax I can’t comment in your answer cause I don’t have enough rep, but I’d say he should scape the pipe, so the regex match exactly the piece of text that he wants
Edit after poster comment on @Qtax answer:
@reveller change your regex as I posted, you have a empty string cause you’re matching the ‘{RT’ before the | and the ‘(\d+)}’ after the pipe, and since the first match don’t have a group to catch up the value it return a empty string.
Changing as I posted you’ll match the entire text so you’ll get it only once and will have the numbers that you need.
Its happening cause a pipe ‘|’ on regex means OR, so you’re matching a ‘{RT’ OR ‘(\d+)}’.
To not match it twice you need to escape the pipe so the regex will match it as text.
This site is a good place to test your regexes, you just put the text you want to test, your regex on another one and you can easily see it working.