我试图找出一个正则表达式来从字符串中提取数字。目前我有字符串:
USD0,13
我只想搜索0,13
.
还会有其他字符串包含数字USD1,14
,例如。
任何帮助都会很棒。干杯
A regex of /USD([0-9,\.]+)/
should do what you want. The dot is not necessarily needed unless you have thousand seperators (considering it looks like you're using EU standards).
If you want multiple currencies, you could do this: /(EUR|GBP|USD)([0-9\,\.]+)/
.
Since you seem pretty new to Regex, I'll explain:
/
is the modifier of the Regex (this can be anything, but we'll use a slash which is the most common character for regexes). If you want it to be incase-sensitive, you can for example do /regex/i
and i
would mean incase-sensitive.
USD
is just a character list of U
followed by S
followed by D
.
(
and )
means that we want the regex to output this match to us.
[0-9,\.]
means numbers from 0
to 9
, commas and dots (note that the dot has to be escaped, as .
in regex means "any character".)
Our match is followed by a +
which means "anything that matches this match, repeated 1 or more times".
The second example we've switched USD
out with (EUR|GBP|USD)
. This means "EUR" or "GBP" or "USD".
If you have any other questions, feel free to ask.
对于您的情况,最简单的一个是:
'/[0-9,]+/'
在 PHP 中
$str = 'USD0,13';
$matches = array();
preg_match('/[0-9,]+/', $str, $matches);
echo $matches[0];