Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marnixk77's avatar

Preg_match new line on windows

I have a string (a url) that is has two possible endings and I would like to extract the number at the end of the first line. If I test my expression in an online tool, it works, but not on my windows desktop.

http://xxxxx/p/3001002990 http://xxxxx/p/3001002990?variant=803713

I have this regex: @(?<=p/)(\d*)(?variant=\d*|\n|\r)@ And if I do a preg_match it only matches on the second line with the variant. If I use this one @(?<=p/)(\d*)@ it matches the first one.

Can some explain why the firstone isn't working. I think its a windows issue with newline/end of line.

0 likes
1 reply
LaryAI's avatar
Level 58

It seems like you're trying to match a number that comes after "p/" and before either the end of the line or the start of the query string "?variant=". The issue you're facing might be related to how newlines are represented in Windows (\r\n) versus Unix-like systems (\n).

You can modify your regular expression to account for both types of line endings. Here's a regex pattern that should work for you:

@(?<=p/)(\d+)(?=\?variant=\d*|[\r\n]|$)@

This pattern uses a positive lookbehind (?<=p/) to ensure that the digits are preceded by "p/". It then captures one or more digits (\d+). Finally, it uses a positive lookahead (?=\?variant=\d*|[\r\n]|$) to assert that what follows is either the start of the query string ?variant=, a newline character (either \r\n for Windows or \n for Unix-like systems), or the end of the string $.

Here's how you can use this pattern in PHP with preg_match:

$urls = "http://xxxxx/p/3001002990
http://xxxxx/p/3001002990?variant=803713";

$pattern = '@(?<=p/)(\d+)(?=\?variant=\d*|[\r\n]|$)@';

preg_match_all($pattern, $urls, $matches);

print_r($matches[0]);

This code will print out all the matches found in the $urls string. The preg_match_all function is used here to find all matches, and $matches[0] will contain the array of numbers that were matched.

Remember that when testing regex patterns on different platforms, line endings can cause issues, so it's always good to account for both \r\n and \n in your patterns when working with multiline strings.

Please or to participate in this conversation.