我想匹配一些在行尾或匹配字符串中间不包含任何分号的字符串。
如果一行:Someword; public void methodName()
或仅public void methodName()
我要匹配:public void methodName()
但是如果行在我匹配的字符串的最后或中间包含分号,如:
Someword;public void; methodName();
或仅public void methodName();
它不匹配任何字符串。到目前为止,我在java中尝试过:
String pattern = "(public)\s+[a-zA-Z]*\s+(\bmethodName\b)\s*\(\)[^;]*$";
Matcher methodMatcher = Pattern.compile(pattern).matcher(classContent); //classContent is a java file
while (methodMatcher.find()) {
System.out.println("methodname="+methodMatcher.group());
}
使用
^(?:w+;)?s*publics+w+s+methodNames*()s*$
参见正则表达式证明。
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
; ';'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
s* whitespace (n, r, t, f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
public 'public'
--------------------------------------------------------------------------------
s+ whitespace (n, r, t, f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
s+ whitespace (n, r, t, f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
methodName 'methodName'
--------------------------------------------------------------------------------
s* whitespace (n, r, t, f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( '('
--------------------------------------------------------------------------------
) ')'
--------------------------------------------------------------------------------
s* whitespace (n, r, t, f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional n, and the end of the
string
样例代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?:\w+;)?\s*public\s+\w+\s+methodName\s*\(\)\s*$";
final String string = "Someword; public void methodName()n"
+ "public void methodName()nn"
+ "Someword; public void; methodName();n"
+ "public void methodName();";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}