【问题标题】:Java Regex - Match line word that not inside stringJava Regex - 匹配不在字符串内的行字
【发布时间】:2016-01-07 13:24:36
【问题描述】:

对于我的项目,我想使用正则表达式而不是字符串中的类

例如

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(?!(\"))")

【问题讨论】:

  • 那太好了,贴上你的帖子,我会接受你的回答。

标签: java regex string class


【解决方案1】:

试试这个正则表达式:

(?:^|\\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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2014-05-02
    • 2021-09-10
    • 2021-06-02
    • 2019-12-07
    • 1970-01-01
    • 2010-11-22
    • 2020-11-13
    • 2019-03-14
    • 1970-01-01
    相关资源
    最近更新 更多