gpt4 book ai didi

java - 在正则表达式中悬挂元字符 + 来检测 GaussianRational

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:56 25 4
gpt4 key购买 nike

public GaussianRational read()  
{
Scanner s = new Scanner(System.in); // associate Scanner object with standard input stream
Long p,q,m,n;
s.findWithinHorizon("(-?\\d+)\\s*/\\s*(-?\\d+)\\s*(\\+? \\s* -?\\d+)\\s*/\\s*(-?\\d+\\s*i)",0);// scan standard input for regular expression pattern

MatchResult result = s.match(); // collect subtrings matched by "capturing groups" in the pattern (in parentheses)
s.close(); // scanner no longer needed
p = Long.parseLong(result.group(1));
q = Long.parseLong(result.group(2));
m = Long.parseLong(result.group(3));
n = Long.parseLong(result.group(4));
System.out.print(p);
return new GaussianRational(p,q,m,n);
}

我正在尝试使用下面的正则表达式从 GaussianRational 输入中解析 4 个长数字

(-?\\d+)\\s*/\\s*(-?\\d+) \\s*(+?)\\s* (-?\\d+)\\s*/\\s*(-?\\d+\\s*i)

但我不断收到表达式中间使用的悬空 + 或悬空 ? 异常

谢谢!

最佳答案

您忘记在+? 之前添加.。如果没有点 + 单独就没有意义,因为它没有转义。

"(-?\\d+)\\s*/\\s*(-?\\d+)\\s*(.+?)\\s*(-?\\d+)\\s*/\\s*(-?\\d+)\\s*i"

如果你真的想加分的话。

"(-?\\d+)\\s*/\\s*(-?\\d+)\\s*\\+?\\s*(-?\\d+)\\s*/\\s*(-?\\d+)\\s*i"

关于java - 在正则表达式中悬挂元字符 + 来检测 GaussianRational,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32905221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com