<span><⼀span> 正则表达式 java

2024-11-02 08:25:27
推荐回答(3个)
回答1:

假设得到的授权羡拿码存于str中


方法一:

直接正颤派激则表达式提取授权码

Pattern pattern = Pattern.compile("\d+");
Matcher matcher = pattern.matcher(str);
String string = matcher.group(1);


方法二:

去除字符串中多余的html标签茄袜

Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(str);
String string = matcher.replaceAll("");

回答2:

方法一

String ResultString = null;

try {

Pattern regex = Pattern.compile("(.*)");

Matcher regexMatcher = regex.matcher(subjectString);

if (regexMatcher.find()) {

ResultString = regexMatcher.group();

} catch (PatternSyntaxException ex) {

// Syntax error in the regular expression

}


如果上述ID不唯一可用以猛裤碰枝谈下方式

List纯清 matchList = new ArrayList();

try {

Pattern regex = Pattern.compile("(.*)");

Matcher regexMatcher = regex.matcher(subjectString);

while (regexMatcher.find()) {

matchList.add(regexMatcher.group());

} catch (PatternSyntaxException ex) {

// Syntax error in the regular expression

}

回答3:

String regEx = "^(.*)

$"谨友氏;
String s = "738626告改 ";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(s);
for(int i=1;i<=mat.groupCount();i++){
  System.out.println(mat.group(i));
}

相关问答