正则表达式帮助:平衡 123,33 页。 => 123.33(双)

发布于 2025-01-01 15:58:44 字数 152 浏览 2 评论 0原文

我是正则表达式的新手。

如何通过正则表达式将字符串解析

Balance 123,45 p.

double 123.45

I'm new in regex..

How can I parse the string through regex

Balance 123,45 p.

to double 123.45

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简单爱 2025-01-08 15:58:44

注意:不是基于正则表达式的解决方案。

对我来说,您获得的字符串似乎来自小数分隔符为 , 而不是 . 的区域设置,您可以执行以下操作:

    NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
    Number n = nf.parse("123,45");
    Double d = n.doubleValue();
    System.out.print(d);

Note: Not a Regex based solution.

To me it seems that the String that you are getting is from a locale where decimal separator is , instead of ., you can just do something like this:

    NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
    Number n = nf.parse("123,45");
    Double d = n.doubleValue();
    System.out.print(d);
追星践月 2025-01-08 15:58:44

您可以使用 String.split()space 作为分隔符,然后使用 String.replaceAll(), 替换为 .

之后使用

返回一个新的双精度值,初始化为由
指定的 String,由 Double 类的 valueOf 方法执行。

String number = "Balance 123,45 p.";
String[] number_array = number.split(" ");

String str = (number_array[1].replaceAll(",","."));
double d = Double.parseDouble(str);
System.out.println(d);

输出=123.45

You can use String.split() with space as a delimilter and than use String.replaceAll() to replace , with ..

After that use parseDouble.It returns,

Returns a new double initialized to the value represented by the
specified String, as performed by the valueOf method of class Double.

String number = "Balance 123,45 p.";
String[] number_array = number.split(" ");

String str = (number_array[1].replaceAll(",","."));
double d = Double.parseDouble(str);
System.out.println(d);

Output = 123.45

最笨的告白 2025-01-08 15:58:44
(\d*(,\d+)?)

这将匹配零个或多个数字,后跟可选的逗号和一个或多个数字。例如:<代码>1,2345; 123; 12,34; ,25(可以省略前导零)

然后您可以用点替换逗号 (string.replace(',', '.')) 并用它执行您想要的操作:)

(\d*(,\d+)?)

This will match zero or more numbers followed by optionally a comma and one or more numbers. e.g: 1,2345; 123; 12,34; ,25 (leading zero may be omitted)

You can then replace the comma with a dot (string.replace(',', '.')) and do what you want with it :)

江南月 2025-01-08 15:58:44

搜索余额 (\d*),(\d*) p\。
替换为 double \$1.\$2

这适用于 IntelliJ IDEA

Search for Balance (\d*),(\d*) p\.
Replace with double \$1.\$2

This works in IntelliJ IDEA

大姐,你呐 2025-01-08 15:58:44
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Double;

public class RegExpTest {
    public static void main(String[] args) {

        String str = "Balance 123,45 p.";

        String p = "Balance (\\d+),(\\d+) p";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()){
             System.out.println(Double.parseDouble(matcher.group(1)+"."+matcher.group(2)));
        }   
    }   
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Double;

public class RegExpTest {
    public static void main(String[] args) {

        String str = "Balance 123,45 p.";

        String p = "Balance (\\d+),(\\d+) p";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()){
             System.out.println(Double.parseDouble(matcher.group(1)+"."+matcher.group(2)));
        }   
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文