Php lookahead statement at the end of regex

I want to write a regex with statements to extract the number 55 from the string unknownstring/55.1 , here is my regex

  $str = 'unknownstring/55.1'; preg_match('/(?<=\/)\d+(?=\.1)$/', $str, $match); 

so basically I'm trying to say to give me the number that appears after the slash, followed by a period and number 1, and after that there are no characters. But this does not match the regular expression. I just tried removing the $ sign from the end and it matched. But this condition is important, since I need it to be the end of the line, because the unknownstring part may contain similar text, for example. unknow/545.1nstring/55.1 . Maybe I can use preg_match_all and take the last match, but I want to understand why the first regular expression does not work, where is my error.

thanks

+5
source share
1 answer

Use $ binding inside lookahead:

 (?<=\/)\d+(?=\.1$) 

RegEx Demo

You cannot use $ outside of a positive look, because your number is NOT at the end of the input, and there is \.1 after it.

+3
source

Source: https://habr.com/ru/post/1209069/


All Articles