"12345" "27462" java正则表达式如何匹配双引号引起来的5位数字,完全匹配。求大神

兄弟帮我讲解下,"12345" "27462" java正则表达式如何匹配双引号引起来的5位数字,完全匹配。求大神?

比如说字符串nfwenlwkngkwleknkwk"12345"nlkknflkwenfk“43678”wefnw0-0-0 我要打印出12345和43678这两个数字
最新回答
心头的小情儿

2024-07-28 05:07:17

package cn.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
public static void orFind(String regexString){
Pattern pattern = Pattern.compile("“\\d{5}”");
Matcher matcher = pattern.matcher(regexString);
String temp = "" ;
while(matcher.find()){
temp = matcher.group().replaceAll("“", "").replaceAll("”", "");
System.out.println(temp);
}
}
public static void main(String[] args) {
String regexString = "nfwenlwkngkwleknkwk\"12345\"nlkknflkwenfk\"43678\"“12345”wefnw0-0-0" ;
Pattern pattern = Pattern.compile("\"\\d{5}\"");
Matcher matcher = pattern.matcher(regexString);
String temp = "" ;
while(matcher.find()){
temp = matcher.group().replaceAll("\"", "");
System.out.println(temp);
}
orFind(regexString);
}
}

//不多说什么,运行结果说话;记得java的字符串中用到双引号“”要用\"转义的
追问
谢谢你!
接住路过的风

2024-07-28 02:44:33

测试通过,任意用!

        Pattern pattern = Pattern.compile("[\"“]{1}(\\d{5})[\"”]{1}");
        Matcher macher = pattern.matcher("nfwenlwkngkwleknkwk\"12345\"nlkknflkwenfk“43678”wefnw0-0-01232");
        
        while (macher.find())
        {
            System.out.println(macher.group(1));
        }
追问
谢谢,正则很好!我点错了最佳答案的。。。
追答
没关系,能帮上你就可以了
酒爷

2024-07-28 05:53:28

public static void main(String[] args) {
    String s = "nfwenlwkngkwleknkwk\"12345\"nlkknflkwenfk“43678”wefnw0-0-0";
    Pattern pattern = Pattern.compile("\\d{5}");
    Matcher m = pattern.matcher(s);
    while (m.find()) {
        System.out.println(m.group(0));
    }
}

 

追问
字符串里面还有其他另外的数字呢,比如我要取{"i":"20130902-034","b":"77200"},{"i":"20130902-035","b":"83584"},{"i":"20130902-036","b":"27530"}这个字符串里面的 77200   83584   27530 这三个数字
追答

你只要把这个字符串当作参数传到pattern.matcher()方法中就可以了

Matcher m = pattern.matcher("{\"i\":\"20130902-034\",\"b\":\"77200\"},{\"i\":\"20130902-035\",\"b\":\"83584\"},{\"i\":\"20130902-036\",\"b\":\"27530\"}");
春夜浅

2024-07-28 00:45:14

Pattern pa=Pattern.compile("\\d{2,}");
Matcher ma=pa.matcher("nfwenlwkngkwleknkwk\"12345\"nlkknflkwenfk“43678”wefnw0-0-0");
while(ma.find()){
System.out.println(ma.group());
}
追问
貌似不行呢 比如我要取{"i":"20130902-034","b":"77200"},{"i":"20130902-035","b":"83584"},{"i":"20130902-036","b":"27530"}这个字符串里面的 77200   83584   27530 这三个数字
追答
public static void test1( ) {
Pattern pa=Pattern.compile("\\d{5}");
Matcher ma=pa.matcher("nfwenlwkngkwleknkwk\"12345\"nlkknflkwenfk“43678”wefnw0-0-0");
while(ma.find()){
System.out.println(ma.group());
}

ma=pa.matcher("{\"i\":\"20130902-034\",\"b\":\"77200\"},{\"i\":\"20130902-035\",\"b\":\"83584\"},{\"i\":\"20130902-036\",\"b\":\"27530\"}");
while(ma.find()){
System.out.println(ma.group());
}
}

Process started >>>
12345
43678
20130
77200
20130
83584
20130
27530
<<< Process finished. (Exit code 0)
追问
大哥 20130这个不应该打印的呀
追答
Pattern pa=Pattern.compile("\\D\\d{5}\\D");
Pattern pa2=Pattern.compile("\\d{5}");
Matcher ma=pa.matcher("nfwenlwkngkwleknkwk\"12345\"nlkknflkwenfk“43678”wefnw0-0-0");
while(ma.find()){
Matcher ma2=pa2.matcher(ma.group());
while(ma2.find()){
System.out.println(ma2.group());
}
}

ma=pa.matcher("{\"i\":\"20130902-034\",\"b\":\"77200\"},{\"i\":\"20130902-035\",\"b\":\"83584\"},{\"i\":\"20130902-036\",\"b\":\"27530\"}");
while(ma.find()){
Matcher ma2=pa2.matcher(ma.group());
while(ma2.find()){
System.out.println(ma2.group());
}
}

Process started >>>
12345
43678
77200
83584
27530
<<< Process finished. (Exit code 0)
浪菊怪哟

2024-07-28 08:38:51

正则表达式[0-9]{5}
追问
字符串中还包含其他无引号引起来的数字
追答
String text = "nfwenlwkngkwleknkwk“12345”nlkknflkwenfk“43678”wefnw0-0-0";
Pattern pattern = Pattern.compile("[0-9]{5}");
Matcher matcher = pattern
.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
你的字符串我将双引号改成中文的了,上面的那位也是对的.
如果数字0也要的话就将[0-9]+或\\d+
Pattern pa = Pattern.compile("[0-9]+");
Matcher ma = pa
.matcher("nfwenlwkngkwleknkwk1234523”nlkknflkwenfk“43678”wefnw0-0-0");
while (ma.find()) {
if(ma.group().length()==5){
System.out.println(ma.group());}
}
追问
谢谢!