There is a regular season:

/(\d{2,4})+|\s+(x|-|na|х|\s|на|)+|\s+(\d{2,4})/ 

She cheers on a 100 на 60 parser and so 100 на 60 .

But for some reason it works on the line: 100 руб

How to modify it so that it works clearly: the numbers (separator) price.

Thank. PS: Php language :)

  • why do you | put? This is OR. So it chooses. Try to use regexr.com for example. helps) - Moonvvell

1 answer 1

I suggest

 '/(\d+)\s*([\p{Pd}xх]|na|на)\s*(\d+)/u' 

See the demo expressions.

  • (\d+) - Group 1: one or more digits
  • \s* - 0+ whitespace characters
  • ([\p{Pd}xх]|na|на) are different hyphens ( \p{Pd} ), x , х , na or
  • \s* - 0+ whitespace characters
  • (\d+) - Group 3: one or more digits

If the numbers can be fractional, you can add (?:[.,]\d+)? after \d+ :

 /(\d+(?:[.,]\d+)?)\s*([\p{Pd}xх]|na|на)\s*(\d+(?:[.,]\d+)?)/u 

PHP:

 $re = '/(\d+)\s*([\p{Pd}xх]|na|на)\s*(\d+)/u'; $str = '100 на 60, 100 руб'; preg_match_all($re, $str, $matches, PREG_SET_ORDER); foreach ($matches as $m) { echo $m[1] . ", " . $m[2] . ", " . $m[3] . "\n"; } // => 100, на, 60 
  • Thank you. !!!!! - php-mvc
  • I am glad to help. Please accept the answer if it solves your problem (see. What to do when someone answered my question? ). - Wiktor Stribiżew
  • And tell me, please, a simple way to separate numbers and letters with a space? Hi1243 string => hi 1243 - php-mvc
  • preg_replace('~(?<=\D)(?=\d)|(?<=\d)(?=\D)~', ' ', $s) - Wiktor Stribiżew
  • And further. How can I specify "may be a space, and maybe it will not be" competently? - php-mvc