简体   繁体   中英

What will be the Regex for my required pattern

I have a pattern like -

public static void myMethod(int Val , String Val){}

so public\\static\\void\\myMethod (int\\sVal\\s,\\sString\\sVal)

but if my method have more space than one like public static it fails. So how to make a concrete pattern .

Moreover the part inside the bracket is not working, suggest me the way to resolve.

Use \\s+ to match one or more occurrence, and \\s* to match zero or more occurrences. Escape the parentheses so that they are not interpreted as grouping operators.

public\s+static\s+void\s+myMethod\s*\(\s*int\s+Val\s*,\s*String\s+Val\s*\)

That said, it looks like you are trying to parse Java code with a regular expression. This is not possible since Java (like the infamous [X]HTML ) is not a regular language.

使用\\s+代替

A few things, but you are on the right track. Replace your \\s with \\s+ to indicate 1 or more whitespace characters.

Also, your parens are not working because they are reserved regex characters. You must escape them to have them be literally interpreted

/public\s+static\s+void\s+myMethod\s*\(\s*int\s+Val\s*,\s*String\s+Val\s*\)/

尝试使用“一个或多个”修饰符( + )匹配多个匹配项:

public\s+static\s+void\s+myMethod...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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