匹配Java中字符串内的浮点数数字,使用正则表达式。

huangapple go评论107阅读模式
英文:

match Float number inside string with regex in Java

问题

我正试图在Java中使用正则表达式查找特定单词后的浮点数,但只有在该单词和浮点数之间没有任何内容时才能找到它,但我希望即使有空格、其他字符和换行符,也能找到它。

以下是我制作的正则表达式:

  1. (?<=TOTAL)([+-]?([0-9]*[.])?[0-9]+)

示例:

  1. > 69003 LYON 03 ejuodnid 04 72.84.75.20 affm groa TICKET FACTURE 361203-
  2. > SEPHORA EYE PALET LIG PALE 29991 14.99 Sephora Collection -Prix 392729
  3. > SEPHORA CINESCOPE
  4. > 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
  5. > Collect1o0 PriX adoy (30 00 1o)o 6.00 oniop20% achats Black Mars 2019
  6. > 451087 OFFRE 20%ACHATS
  7. > 15.00 MASC
  8. > 16.50
  9. > 3.50
  10. > 3.00 N&#39;2
  11. > 24.00 VPBLA
  12. > 0.00 tnoe 0001* 1eepom TOTAL EUR
  13. > 62.00
英文:

I am trying to find a float number after a specific word with regex in java , but I am only getting it when there is nothing between the word and the float number , but I want to get it even there are white spaces any other characters and new lines new lines .

Here the regex that I made :

  1. (?&lt;=TOTAL)([+-]?([0-9]*[.])?[0-9]+)

Example :

> 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

答案1

得分: 2

使用

  1. \bTOTAL\b[\s\S]*?([+-]?\d*\.?\d+)

参见证明

解释

  1. --------------------------------------------------------------------------------
  2. \b 单词字符 (\w) 与非单词字符之间的边界
  3. --------------------------------------------------------------------------------
  4. TOTAL 'TOTAL'
  5. --------------------------------------------------------------------------------
  6. \b 单词字符 (\w) 与非单词字符之间的边界
  7. --------------------------------------------------------------------------------
  8. [\s\S]*? 任何字符:空白字符 (\n\r\t\f 和空格),非空白字符 (\n\r\t\f 和非空格)(0 次或多次,匹配最少的次数)
  9. --------------------------------------------------------------------------------
  10. ( 分组并捕获至
  11. --------------------------------------------------------------------------------
  12. [+-]? 任何字符:'+''-'(可选,匹配最多的次数)
  13. --------------------------------------------------------------------------------
  14. \d* 数字 (0-9)(0 次或多次,匹配最多的次数)
  15. --------------------------------------------------------------------------------
  16. \.? '.'(可选,匹配最多的次数)
  17. --------------------------------------------------------------------------------
  18. \d+ 数字 (0-9)(1 次或多次,匹配最多的次数)
  19. --------------------------------------------------------------------------------
  20. ) 的结尾
  21. Java 代码:
  22. ```java
  23. String regex = "\\bTOTAL\\b[\\s\\S]*?([+-]?\\d*\\.?\\d+)";
  24. 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";
  25. Pattern pattern = Pattern.compile(regex);
  26. Matcher matcher = pattern.matcher(string);
  27. if (matcher.find()) {
  28. System.out.println(matcher.group(1));
  29. }
  30. 结果: `62.00`
  31. [1]: https://regex101.com/r/0ryQ4h/1
英文:

Use

  1. \bTOTAL\b[\s\S]*?([+-]?\d*\.?\d+)

See proof

Explanation

  1. --------------------------------------------------------------------------------
  2. \b the boundary between a word char (\w) and
  3. something that is not a word char
  4. --------------------------------------------------------------------------------
  5. TOTAL &#39;TOTAL&#39;
  6. --------------------------------------------------------------------------------
  7. \b the boundary between a word char (\w) and
  8. something that is not a word char
  9. --------------------------------------------------------------------------------
  10. [\s\S]*? any character of: whitespace (\n, \r, \t,
  11. \f, and &quot; &quot;), non-whitespace (all but \n,
  12. \r, \t, \f, and &quot; &quot;) (0 or more times
  13. (matching the least amount possible))
  14. --------------------------------------------------------------------------------
  15. ( group and capture to :
  16. --------------------------------------------------------------------------------
  17. [+-]? any character of: &#39;+&#39;, &#39;-&#39; (optional
  18. (matching the most amount possible))
  19. --------------------------------------------------------------------------------
  20. \d* digits (0-9) (0 or more times (matching
  21. the most amount possible))
  22. --------------------------------------------------------------------------------
  23. \.? &#39;.&#39; (optional (matching the most amount
  24. possible))
  25. --------------------------------------------------------------------------------
  26. \d+ digits (0-9) (1 or more times (matching
  27. the most amount possible))
  28. --------------------------------------------------------------------------------
  29. ) end of

Java code:

  1. String regex = &quot;\\bTOTAL\\b[\\s\\S]*?([+-]?\\d*\\.?\\d+)&quot;;
  2. String string = &quot;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&#39;2 24.00 VPBLA 0.00 tnoe 0001* 1eepom TOTAL EUR 62.00&quot;;
  3. Pattern pattern = Pattern.compile(regex);
  4. Matcher matcher = pattern.matcher(string);
  5. if (matcher.find()) {
  6. System.out.println(matcher.group(1));
  7. }

Result: 62.00

答案2

得分: 1

我理解这个问题是你想要从特定单词之后提取第一个浮点数,无论中间有什么内容。
一个非贪婪通配符就可以为你完成这个任务。

(?<=TOTAL).*?([+-]?([0-9]*[.])?[0-9]+)

英文:

I interpret the question as that you want to extract the first float number after a certain word, no matter what is in between.
A non-greedy wildcard will simply do that for you.

(?&lt;=TOTAL).*?([+-]?([0-9]*[.])?[0-9]+)

huangapple
  • 本文由 发表于 2020年9月15日 03:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63890607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定