Java正则表达式从字符串拆分为双倍 [英] Java regex split double from string

查看:211
本文介绍了Java正则表达式从字符串拆分为双倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决以下字符串中的问题:

I am having a problem splitting something like the following string:

43.80USD

我想要什么是将表达式分解为具有43.80作为第一个元素的数组,USD作为第二个元素。所以结果会是这样的:

What I want is to be able to split the expression into an array that has "43.80" as the first element and "USD" as the second. So the result would be something like:

[43.80,USD]

["43.80", "USD"]

我确定有一些方法可以使用正则表达式,但是我不够精通它自己来计算出来。任何帮助将不胜感激。

I am sure there is some way to do this with regex, but I am not proficient enough with it to figure it out on my own. Any help would be much appreciated.

推荐答案

如果您的字符串的格式是固定的,您可以按如下方式拆分:

If the format of your string is fixed you can split it as follows

String[] currency = "48.50USD".split("(?<=\\d)(?=[a-zA-Z])");
System.out.println("Amount='"+currency[0]+"'; Denomination='"+currency[1]+"'");
// prints: Amount='48.50'; Denomination='USD'

上面的正则表达式使用 正面看法 (?< =)和 positive lookahead (?=)找到一个分隔符(这是长度此处)前面带有一个数字,后跟一个字母。

The regex above uses a positive look-behind (?<=) and a positive lookahead (?=) to find a separator (which is of zero-length here) that's preceded with a number and followed by a letter.

这篇关于Java正则表达式从字符串拆分为双倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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