我有一些表格的数据项:
blahblahblahP26
blahblahblahP82982
blahblahblahP0
blahblahblahP913
我知道java模式匹配器与普通的正则表达式有点不同。
我想做的就是抓住P之后的所有内容。不多也不少。
怎么做?
答案 0 :(得分:3)
你不需要正则表达式。尝试使用substring
String afterP= str.substring(str.indexOf('P')+1);
// Add 1 if don't want to include P
如果P
出现多次,则可以使用lastIndexOf('P')
答案 1 :(得分:1)
Pattern p=Pattern.compile("P[0-9]+");
Matcher m=p.matcher(inp);
while(m.find()){
System.out.println(inp.substring(m.start(), m.end()));
}
在inp中使用此正则表达式传递要解析的String。它适用于变量整数,后跟P
答案 2 :(得分:0)
我清除了P
之前的所有内容。有了这个,你还可以处理最后一个之前可能的P
:
String afterP = str.replaceAll("^.*P", "");
答案 3 :(得分:0)
听起来你已经用其他语言学习了正则表达式,那么this应该足以让你学习java正则表达式。如果您是regex的新手,这个tutorial非常详细。 对于你的情况,这应该有效:
Pattern pattern = Pattern.compile(".*?(P.*)"); // question mark is Reluctant quantifiers , it fetch sequence as short as possible.
Matcher matcher = pattern.matcher("blahblahblahP26");
if(matcher.matches()){
System.out.println(matcher.group(1));
}