如何在Java中使用正则表达式从标志(sign)中提取内容?

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

How to extract content from sign using regex in Java?

问题

你的代码中有一些问题。以下是修正后的代码:

static java.util.regex.Pattern p1=java.util.regex.Pattern.compile("\$\\{\\w+\\}");
private static String getStudentName(String expression) {
    StringBuffer stringBuffer = new StringBuffer();
    java.util.regex.Matcher m1 = p1.matcher(expression);
    while(m1.find()) {
        String param = m1.group();
        stringBuffer.append(param.substring(2, param.indexOf('.')) + ",");
    }
    if(stringBuffer.length() > 0){
        return stringBuffer.deleteCharAt(stringBuffer.length() - 1).toString();
    }
    return  null;
}

注意到我已经更新了正则表达式和 param 的提取方式,以及对 substring 的调用,以确保能正确提取出 studentAstudentB

英文:

My content is a string like this:whoad123@@${studentA.math1.math2}dsafddasfd${studentB.math2.math3},now I want to extract the content studentA,studentB which is in the braces and before the first pot(${**}).What's wrong with my code?

static java.util.regex.Pattern p1=java.util.regex.Pattern.compile("\\*\$\\{\\w+\\}");
private static String getStudentName(String expression) {
    StringBuffer stringBuffer = new StringBuffer();
    java.util.regex.Matcher m1= p1.matcher(expression);
    while(m1.find()) {
        String param=m1.group(1);
        stringBuffer.append(param.substring(2,param.indexOf("\\.")) + ",");
    }
    if(stringBuffer.length()>0){
        return stringBuffer.deleteCharAt(stringBuffer.length()-1).toString();
    }
    return  null;
}

答案1

得分: 1

Use

${([^{}.]+)

See proof

Declare in Java as

static java.util.regex.Pattern p1=java.util.regex.Pattern.compile("\$\\{([^{}.]+)");
英文:

Use

$\{([^{}.]+)

See proof

Declare in Java as

static java.util.regex.Pattern p1=java.util.regex.Pattern.compile("\$\\{([^{}.]+)");

EXPLANATION

                         EXPLANATION
--------------------------------------------------------------------------------
  $                       '$'
--------------------------------------------------------------------------------
  \{                       '{'
--------------------------------------------------------------------------------
  (                        group and capture to :
--------------------------------------------------------------------------------
    [^{}.]+                  any character except: '{', '}', '.' (1
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of 

答案2

得分: 1

public static String getStudentName(String expression) {
    Pattern pattern = Pattern.compile("\\{(?<student>\\w+)\\.[^\\}]+\\}");
    Matcher matcher = pattern.matcher(expression);
    List<String> names = new ArrayList<>();

    while (matcher.find()) {
        names.add(matcher.group("student"));
    }

    return String.join(",", names);
}

See demo in [regex101.com][1]
英文:
public static String getStudentName(String expression) {
    Pattern pattern = Pattern.compile(&quot;\\{(?&lt;student&gt;\\w+)\\.[^\\}]+\\}&quot;);
    Matcher matcher = pattern.matcher(expression);
    List&lt;String&gt; names = new ArrayList&lt;&gt;();

    while (matcher.find()) {
        names.add(matcher.group(&quot;student&quot;));
    }

    return String.join(&quot;,&quot;, names);
}

See demo in regex101.com

huangapple
  • 本文由 发表于 2020年8月19日 01:25:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63473693.html
匿名

发表评论

匿名网友

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

确定