简体   繁体   English

带有=和a的正则表达式;

[英]Regular expression with an = and a ;

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 characters in between. 我正在尝试使用正则表达式来查找以等号( = )开头的所有子串,并以分号( ; )结尾,其中包含任意数量的字符。 It should be something like this =*; 它应该是这样的东西=*;

For some reason, the equals is not registering. 出于某种原因,等于没有注册。 Is there some sort of escape character that will make the regex notice my equals sign? 是否有某种逃避角色会使正则表达式注意到我的等号?

I'm working in Java if that has any bearings on this question. 我正在使用Java,如果这个问题有任何影响的话。

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. 我相信第一组是整场比赛, group[1]是在括号内找到的组。

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 ';'.
}

This looks for "any number of = signs, including 0" 这会查找“任意数量的=符号,包括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: 但是,这将贪婪地匹配 - 如果你想要延迟匹配(以便它在找到下一个分号时停止一个组)你可能需要:

=.*?;

The regex you provided would match ; 你提供的正则表达式会匹配; , ===; ===; , ..., ================; ,..., ================; . How about =.*; =.*; (or =.*?; if non-greedy is needed)? (或者=.*?;如果需要非贪婪的话)?

像=。*;

学习Java中regexp的一个很好的资源: sun的关于regexp的书

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM