简体   繁体   中英

regex preg_replace php

I have random variable like: Strip @ 489.000 Strip 1 @ 489.000 Strip 2 @ 589.000

I need output will be: only number after 'anything @ ' 489.000

so give me output: 489.000 489.000 589.000

hot to achive this use php regex?

$string = '  Strip 1 @ 489.000'; $pattern = ' /(\s\S) @ (\d+)/i'; $replacement = '$3'; echo preg_replace($pattern, $replacement, $string);

To get all matches, use

if (preg_match_all('/\S\s@\s+\K\d+(?:\.\d+)?/', $text, $matches)) {
    print_r($matches[0]);
}

To get the first match, use

if (preg_match('/\S\s@\s+\K\d+(?:\.\d+)?/', $text, $match)) {
    print_r($match[0]);
}

Details

  • \\S - a non-whitespace char
  • \\s - a whitespace
  • @ - a @ char
  • \\s+ - 1+ whitespaces
  • \\K - match reset operator
  • \\d+ - 1+ digits
  • (?:\\.\\d+)? - an optional sequence of a dot and 1+ digits.

See the regex demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM