繁体   English   中英

Java Regex-匹配不在字符串内的行词

[英]Java Regex - Match line word that not inside string

对于我的项目,我想使用正则表达式获取类,而不是在字符串内部

例如

class fo{
    void foo{
        System.out.println("example writing of class");
        System.out.println("class cls{");
    }
}

所以,我希望这样的结果:

class fo{

我尝试创建一个模式,但无法正常工作,在这里。

Pattern.compile("\\b(?!(\"))class\\s+\\w+\\{\\b(?!(\"))")

试试这个正则表达式:

(?:^|\\n)\\s*class.*

解释:

(?:^|\\n)       # from start or new line
\\s*            # as many as possible spaces
class           # the 'class' text
.*              # all characters till the end of line

希望能帮助到你。

暂无
暂无

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

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