字符串中整数的出现 [英] Occurences for integer in string
问题描述
package RegularEspressions;
import java.util.regex。*;
公共类RegFndMultiple {
public static void main(String args []){
Pattern pat = Pattern.compile(^([+ - ]?[0-9] ] \\d * | 0)$);
Matcher mat = pat.matcher(word 1 2 3 android);
while(mat.find()){
System.out.println(在索引处找到的数字+
mat.start());
}
}
}
应显示数字位置,代码打印整数位置
我尝试了什么:
更改了代码并添加了新模式
);
Matcher mat = pat.matcher(word 1 2 3 android);
while(mat.find()){
System.out.println(在索引处找到的数字+
mat.start());
}
}
}
应该显示数字的位置,打印整数位置的代码
我尝试了什么:
改变了代码并添加了新模式
你的模式是错误的,它只会匹配包含整数的行,而不是因为 ^ 和
。
模式会更好
< pre lang =text>([+ - ]?[0-9] \\d * | 0)
在任何情况下,模式都过于复杂,我会使用
([+ - ] ?\\\\ +)
只是一些有趣的链接来帮助构建和调试RegEx。
以下是RegEx文档的链接:
perlre - perldoc.perl.org [< a href =http://perldoc.perl.org/perlre.htmltarget =_ blanktitle =New Window> ^ ]
这是指向工具的链接帮助构建RegEx并调试它们:
.NET Regex Tester - Regex Storm [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr:Learn,Build,&测试RegEx [ ^ ]
此节目RegEx是一个很好的图表,它非常有助于理解RegEx的作用:
Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
[更新]
尝试我的模式和你的解决方案测试1 23 -45测试
你可能会感到惊讶
package RegularEspressions; import java.util.regex.*; public class RegFndMultiple { public static void main(String args[]) { Pattern pat = Pattern.compile("^([+-]?[0-9]\\d*|0)$"); Matcher mat = pat.matcher("word 1 2 3 android"); while(mat.find()) { System.out.println("numbers found at index " + mat.start()); } } }
should display position of numbers , code that prints the position of integers
What I have tried:
changed code and added new patterns
"); Matcher mat = pat.matcher("word 1 2 3 android"); while(mat.find()) { System.out.println("numbers found at index " + mat.start()); } } }
should display position of numbers , code that prints the position of integers
What I have tried:
changed code and added new patterns
Your pattern is wrong, it will match only lines containing an integer and nothing else because of ^ and
.
Pattern would be better as
"([+-]?[0-9]\\d*|0)"
In any cases, the pattern is over complicated, I would use
"([+-]?\\d+)"
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
[Update]
Try my pattern and your solution with "test 1 23 -45 test"
You may get surprised.
这篇关于字符串中整数的出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!