PHP正则表达式一起捕获货币和数字[关闭]

I am new to coding.

There is a problem to get the numbers from a sentence. I tried to find but unfortunately found nothing at all.

for example: usd5 for potatoes. -> i want to catch "usd5" with php

How can I do this?

try this.u will get the result as an array

<?php

$str = 'usd5 for potatoes';

preg_match('/(?P<name>\w+)(?P<digit>\d+)/', $str, $matches);

print_r($matches);

?>

u will get currency from $matches['name'] and value from $matches['digit']

Try this

$str = 'usd5 for potatoes.';
$regex = '/(usd|gbp)\d/';
preg_match($regex, $str, $match);
var_dump($match);

This website offers an excellent introduction to regular expressions

http://www.regular-expressions.info/tutorial.html