简体   繁体   English

字符串的 JFLEX 正则表达式以

[英]JFLEX regular expressions for string starts with

I'm new with this jflex and regular expressions I read a lot even though I know I faced the solution in the manual but really I can't understand these regular expressions.我是这个 jflex 和正则表达式的新手,我读了很多书,尽管我知道我遇到了手册中的解决方案,但我真的无法理解这些正则表达式。 I want simple read a text file with jflex and return every line that starts with name = or name= but this was a pain for me for 1 week I'm reading all regular expressions in the jflex document but I can't understand which one I can use to achieve it anyone can help me out?我想用 jflex 简单地读取一个文本文件并返回以name =name=开头的每一行,但这对我来说是一个痛苦的 1 周我正在阅读 jflex 文档中的所有正则表达式,但我不明白是哪一个我可以用它来实现它任何人都可以帮助我吗? with jflex rules and any code to contain that?使用 jflex 规则和包含该规则的任何代码?

this is the example input and output这是示例输入和输出

bla bla bla
bla bla bla
name=StackOverFlow
name = ThisIsGo
bla bla bla

Output:输出:

StackOverFLow堆栈溢出

ThisIsGo这就是Go

If you need to match something placed after the equal sign preceded by the word name , then you could simply describe that syntax and use a capturing group to enclose the value after the equal sign.如果您需要匹配放在等号后面的内容,前面有单词name ,那么您可以简单地描述该语法并使用捕获组将等号后面的值括起来。

name\s*=\s*(.+)

Test Link测试链接

Here you can test the regex above:在这里你可以测试上面的正则表达式:

https://regex101.com/r/FMXSDK/1 https://regex101.com/r/FMXSDK/1

Code Snippet代码片段

Your code would look like this:您的代码如下所示:

String str = "bla bla bla\n" +
        "bla bla bla\n" +
        "name=StackOverFlow\n" +
        "name = ThisIsGo\n" +
        "bla bla bla";

Pattern pattern = Pattern.compile("name\\s*=\\s*(.+)");
Matcher matcher = pattern.matcher(str);

while (matcher.find()){
    System.out.println(matcher.group(1));
}

Output输出

StackOverFlow
ThisIsGo

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

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