简体   繁体   English

regexp来匹配。 和空白行

[英]regexp to match text between . " and blank line

i'm new to regexp and I'm stuck with trivial parsing task; 我是regexp的新手,并且对琐碎的解析任务感到困惑; Giving following structure i need regexp that matches all text between empty lines, so i could iterate through array of matches, and parse each item as a question 提供以下结构,我需要匹配空行之间所有文本的正则表达式,因此我可以遍历匹配数组,并将每个项目解析为一个问题

id1. "question title 1"
* variant one
* variant two
* three // comment

id2. "question title 2"
+ variant one
+ two
+ three

id3. "Anoter question title"
another variant

You can use \\. ".* 您可以使用\\. ".* \\. ".* this will give you . "question title 1" as one of the match. You can read from fourth char to last but one. \\. ".*这将为您提供” . "question title 1"作为匹配项之一。您可以从第四个字符读取到最后一个字符。

I couldn't figure out a way to match both question and answer line using regular expression. 我想不出一种使用正则表达式同时匹配问题和答案行的方法。 Because, this regular expression will match multiple lines but \\. "[\\s\\S]+ 因为,此正则表达式将匹配多行,但\\. "[\\s\\S]+ \\. "[\\s\\S]+ but it will match until end of the string matching all questions and answers into a single string. \\. "[\\s\\S]+但它将匹配到匹配所有问题和答案的单个字符串为止。

If we assume that the answers will always start with either + or *, Another alternative is to use (\\. ".*)|([\\*\\+].*) This will match both questions and answers. However questions and answers will be separate matches. 如果我们假设答案总是以+或*开头,则另一种替代方法是使用(\\. ".*)|([\\*\\+].*)这将匹配问题和答案。将是单独的比赛。

Another way is to split the string using id. " 另一种方法是使用id. "拆分字符串id. " id. " This will give each of your question and answer into another string. However you may have to remove the " at the end of the question manually. id. "这将把您的每个问题和答案放入另一个字符串中。但是,您可能必须手动删除问题末尾的"

Another way I can imagine is splitting entire string using ^\\s*$ . 我可以想象的另一种方法是使用^\\s*$拆分整个字符串。 This will split the string using blank line and give you both question and answer as one single group. 这将使用空行分割字符串,并将问题和答案作为一个单独的组。 I hope this helps. 我希望这有帮助。

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

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