Regular expression with an = and a ;

前端 未结 5 952
情深已故
情深已故 2021-02-12 15:02

I\'m trying to use a regular expression to find all substrings that start with an equals sign (=) and ends with a semicolon (;) with any number of char

相关标签:
5条回答
  • 2021-02-12 15:12

    This looks for "any number of = signs, including 0"

    =*;
    

    If you want "= followed by any number of other characters" you want

    =.*;
    

    However, that will match greedily - if you want lazy matching (so that it stops one group when it finds the next semicolon) you might want:

    =.*?;
    
    0 讨论(0)
  • 2021-02-12 15:24

    Something like =.*;

    0 讨论(0)
  • 2021-02-12 15:33

    This may be what you are looking for. You need to specify a character set or wild card character that you are applying the asterisk to.

    "=([^;]*);"
    

    You can also use the reluctant quantifier:

    "=(.*?);"
    

    Using the parenthesis you now have groups. I believe the first group is the whole entire match, and group[1] is the group found within the parenthesis.

    The code may look something like:

    Regex r = new Regex("=([^;]*);");
    Match m = r.Match(yourData);
    while (m.Success) {
        string match = m.Groups[1];
        // match should be the text between the '=' and the ';'.
    }
    
    0 讨论(0)
  • 2021-02-12 15:34

    The regex you provided would match ;, ===;, ..., ================;. How about =.*; (or =.*?; if non-greedy is needed)?

    0 讨论(0)
  • 2021-02-12 15:34

    An excellent source for learning about regexp in Java: sun's book about regexp

    0 讨论(0)
提交回复
热议问题