除数字和货币分母外,从字符串中剥离所有内容 [英] Strip everything from a string apart from a number and currency denominator

查看:83
本文介绍了除数字和货币分母外,从字符串中剥离所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例字符串:

The price is $54.00 including delivery
On sale for £12.99 until December
European pricing €54.76 excluding UK

我只想返回每个价格和货币分母

From each of them I want to return only the price and currency denominator

$54.00
£12.99
€54.76

我的虽然过程是拥有一组货币符号,然后为每个字符搜索字符串,然后在其后仅捕获空格之前的字符-但是,$ 67.00将失败

My though process is the have an array of currency symbols and search the string for each one and then capture just the characters before the space after that - however, $ 67.00 would then fail

因此,我可以遍历一组预设货币符号,然后将字符串爆炸并在下一个非数字字符(不是)的情况下切碎.或-或使用正则表达式

So, can i run through an array of preset currency symbols, then explode the string and chop it at the next instance of a non numeric character that is not a . or , - or maybe with regex

这可能吗?

推荐答案

在正则表达式中,\p{Currency_Symbol}\p{Sc}代表任何货币符号.

In regex, \p{Currency_Symbol} or \p{Sc} represent any currency symbol.

但是, PHP支持仅是速记形式/u修饰符是必需的.

However, PHP supports only the shorthand form \p{Sc} and /u modifier is required.

使用正则表达式模式

/\p{Sc}\s*\d[.,\d]*(?<=\d)/u

您将能够进行匹配,例如:

you will be able to match for example:

  • $ 1,234
  • 12.3英镑
  • €5,345.01

如果要将.用作小数点分隔符并将,用作千位定界符,请使用

If you want to use . as a decimal separator and , as a thousands delimiter, then go with

/\p{Sc}\s*\d{1,3}(?:,\d{3})*(?:\.\d+)?/u


检查 此演示 .


Check this demo.

这篇关于除数字和货币分母外,从字符串中剥离所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆