用Java中的正则表达式匹配字符串中的浮点数
我试图用java中的正则表达式在一个特定的单词后找到一个浮点数,但我只在单词和浮点数之间没有任何内容时才得到它,但我想得到它,即使有空格、任何其他字符和新行 这里是我制作的正则表达式:用Java中的正则表达式匹配字符串中的浮点数,java,regex,Java,Regex,我试图用java中的正则表达式在一个特定的单词后找到一个浮点数,但我只在单词和浮点数之间没有任何内容时才得到它,但我想得到它,即使有空格、任何其他字符和新行 这里是我制作的正则表达式: (?<=TOTAL)([+-]?([0-9]*[.])?[0-9]+) (?我将这个问题解释为,您希望提取某个单词后面的第一个浮点数,而不管两者之间是什么。 一个非贪婪的通配符只会帮你做到这一点 (?我将这个问题解释为,您希望提取某个单词后面的第一个浮点数,而不管两者之间是什么。 一个非贪婪的通配符只会帮
(?<=TOTAL)([+-]?([0-9]*[.])?[0-9]+)
(?我将这个问题解释为,您希望提取某个单词后面的第一个浮点数,而不管两者之间是什么。
一个非贪婪的通配符只会帮你做到这一点
(?我将这个问题解释为,您希望提取某个单词后面的第一个浮点数,而不管两者之间是什么。
一个非贪婪的通配符只会帮你做到这一点
(?使用
看
解释
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
TOTAL 'TOTAL'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
[\s\S]*? any character of: whitespace (\n, \r, \t,
\f, and " "), non-whitespace (all but \n,
\r, \t, \f, and " ") (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[+-]? any character of: '+', '-' (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \1
Java代码:
String regex = "\\bTOTAL\\b[\\s\\S]*?([+-]?\\d*\\.?\\d+)";
String string = "69003 LYON 03 ejuodnid 04 72.84.75.20 affm groa TICKET FACTURE 361203- SEPHORA EYE PALET LIG PALE 29991 14.99 Sephora Collection -Prix 392729 SEPHORA CINESCOPE 16.501 328451- SEPHORA THE MASC BIG MASC goe( 6.99193.49 P Sephora Co11ection Prix 347597SEPHORA LING GRENADE NG 25 i5.99 1) 2.99 Sephora Collect1o0 PriX adoy (30 00 1o)o 6.00 oniop20% achats Black Mars 2019 451087 OFFRE 20%ACHATS 15.00 MASC 16.50 3.50 3.00 N'2 24.00 VPBLA 0.00 tnoe 0001* 1eepom TOTAL EUR 62.00";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
结果:62.00
使用
\bTOTAL\b[\s\S]*?([+-]?\d*\.?\d+)
看
解释
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
TOTAL 'TOTAL'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
[\s\S]*? any character of: whitespace (\n, \r, \t,
\f, and " "), non-whitespace (all but \n,
\r, \t, \f, and " ") (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[+-]? any character of: '+', '-' (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \1
Java代码:
String regex = "\\bTOTAL\\b[\\s\\S]*?([+-]?\\d*\\.?\\d+)";
String string = "69003 LYON 03 ejuodnid 04 72.84.75.20 affm groa TICKET FACTURE 361203- SEPHORA EYE PALET LIG PALE 29991 14.99 Sephora Collection -Prix 392729 SEPHORA CINESCOPE 16.501 328451- SEPHORA THE MASC BIG MASC goe( 6.99193.49 P Sephora Co11ection Prix 347597SEPHORA LING GRENADE NG 25 i5.99 1) 2.99 Sephora Collect1o0 PriX adoy (30 00 1o)o 6.00 oniop20% achats Black Mars 2019 451087 OFFRE 20%ACHATS 15.00 MASC 16.50 3.50 3.00 N'2 24.00 VPBLA 0.00 tnoe 0001* 1eepom TOTAL EUR 62.00";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
结果:62.00
请发布一些示例数据只需使用:(?s)\bTOTAL\b.*([+-])?\d+(?:\。\d+)
并获取组1FYI:javadoc中显示了用于匹配浮点数的完整正则表达式。请发布一些示例数据只需使用:(?s)\bTOTAL\b.*([+]?\d+)(?:\。。。。\。。)
和get group#1FYI:javadoc中显示了匹配浮点数的完整正则表达式。否,它无效。:)编辑时有一个拼写错误,我现在已将其更改回。否,它无效。:)编辑时有一个拼写错误,我现在已将其更改回。