3 回答

TA贡献1827条经验 获得超9个赞
您的正则表达式分析:
"ˆ([\\d]+(\\.[\\d]{2})?\\|([A-Z]{2}){1})(,[A-Z]{2})*\\s(\\\".+\\\")?$"
首先,让我们将 Java 字符串文字反转义为实际的正则表达式字符串:
ˆ([\d]+(\.[\d]{2})?\|([A-Z]{2}){1})(,[A-Z]{2})*\s(\".+\")?$
现在让我们把它分开:
ˆ Incorrect character 'ˆ', should be '^'
Match start of input, but your input starts with '['
(
[\d]+ The '[]' is superfluous, use '\d+'
(\.[\d]{2})? Don't capture this, use '(?:X)?'
\|
([A-Z]{2}){1} The '{1}` is superfluous, and don't capture just this
) You're capturing too much. Move back to before '\|'
(,[A-Z]{2})* Will only capture last ',XX'.
Use a capture group around all the letters, then split that on ','
\s
(\".+\")? No need to escape '"', and only capture the content
$ Match end of input, but your input ends with ']'
因此,清理后它将是:
^\[
(
\d+
(?:\.[\d]{2})?
)
\|
(
[A-Z]{2}
(?:,[A-Z]{2})*
)
\s
(?:"(.+)")?
\]$
一起回来:
^\[(\d+(?:\.[\d]{2})?)\|([A-Z]{2}(?:,[A-Z]{2})*)\s(?:"(.+)")?\]$
使用[15.00|GR,LQ,MD "Uber"]
将捕获的输入:
15.00
- 全数GR,LQ,MD
-split(",")
用于获取数组{ "GR", "LQ", "MD" }
Uber
- 只是没有引号的文字
请参阅regex101.com 上的演示。

TA贡献1847条经验 获得超11个赞
第一个字符是 aˆ
而不是^
。除此之外,您应该将第一组更改([\d]+(\.[\d]{2})?)
为 get only15.00
和 not 15.00|GR
。
完整示例如下所示:
Pattern.compile("^([\\d]+(\\.[\\d]{2})?)\\|(([A-Z]{2})(,[A-Z]{2})*)\\s(\".+\")?$");

TA贡献1835条经验 获得超7个赞
有两个主要问题。
该
ˆ
字符是重音抑扬符而不是^
插入符号。您没有在正则表达式中包含方括号。
一个可能的解决方案可能是这样的
Pattern.compile("^\\[(?<number>[\\d]+(?>\\.[\\d]{2})?)\\|(?<codes>(?>[A-Z]{2},?)+)(?>\\s\\\"(?<comment>.+)\\\")?\\]$");
该解决方案还命名了捕获组,这使得指定您希望从哪个组获取价值变得更好。https://regex101.com/r/HEboNf/2
所有三个 2 字母代码都分组在一个捕获组中,您可以在代码中用逗号分隔它们。
添加回答
举报