简体   繁体   English

正则表达式获取 Markdown 文件中的部分

[英]Regex get section in markdown file

I am trying to use regex to get each section of a list of links ( https://raw.githubusercontent.com/Anything-Minecraft-Team/anything-minecraft/main/server/info/lists/plugins/anticheats.md )我正在尝试使用正则表达式来获取链接列表的每个部分( https://raw.githubusercontent.com/Anything-Minecraft-Team/anything-minecraft/main/server/info/lists/plugins/anticheats.md

I want to be able to get each section from the list like this我希望能够像这样从列表中获取每个部分

- [Anti Cheat](https://github.com/Paroxial/Anti-Cheat)  
Version: 1.8  
Rating: ???  
Discontinued

so I can get the version/rating info and know what link it came from所以我可以获得版本/评级信息并知道它来自哪个链接

I have (?:.*)(?:\\n .*)* that selects text that has spaces in between above/below text (edit no it doesnt im really dumb).我有(?:.*)(?:\\n .*)*选择文本上方/下方之间有空格的文本(编辑不,它不是我真的很笨)。 But due to my formatting it doesn't work, is there another way to do this or do I have to change the formatting?但是由于我的格式不起作用,是否有另一种方法可以做到这一点,或者我是否必须更改格式?

You can get the link and the sections using 3 capture groups.您可以使用 3 个捕获组获取链接和部分。

^- \[[^]\[]+]\((https?://[^\s()]+)\).*\R(Version:.*)\R(Rating:.*)\R(\S.+)$
  • ^- Match - at the start of the string ^-匹配-在字符串的开头
  • \\[[^]\\[]+] Match from [...] \\[[^]\\[]+]匹配来自[...]
  • \\((https?://[^\\s()]+)\\) Match ( and capture the url in group 1 and match ) \\((https?://[^\\s()]+)\\)匹配(并捕获第 1 组中的 url 并匹配)
  • .*\\R Match the rest of the line and a newline .*\\R匹配行的其余部分和换行符
  • (Version:.*) Capture group 2, match the Version information (Version:.*)捕获组2,匹配版本信息
  • \\R(Rating:.*) Capture group 3, match the Rating information \\R(Rating:.*)捕获第3组,匹配Rating信息
  • \\R(\\S.+)$ Match a newline and capture a single whitespace char and the rest of the line in group 4 \\R(\\S.+)$匹配换行符并捕获单个空格字符和组 4 中的其余行

Regex demo |正则表达式演示| Java demo Java 演示

final String regex = "^- \\[[^]\\[]+]\\((https?://[^\\s()]+)\\).*\\R(Version:.*)\\R(Rating:.*)\\R(\\S.+)$";
final String string = "- [ABC Advanced Anticheat](https://www.spigotmc.org/resources/91606/) - Removed due to private reasons  \n"
+ "Version: 1.7 - 1.16  \n"
+ "Rating: 4  \n"
+ "Discontinued\n"
+ "- [AbdeslamNeverCheat](https://www.spigotmc.org/resources/61280)  \n"
+ "Version: 1.8  \n"
+ "Rating: 1  \n"
+ "Discontinued";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Link: " + matcher.group(1));
    System.out.println("Version: " + matcher.group(2));
    System.out.println("Rating: " + matcher.group(3));
    System.out.println("Status: " + matcher.group(4));
}

Output输出

Link: https://www.spigotmc.org/resources/91606/
Version: Version: 1.7 - 1.16  
Rating: Rating: 4  
Status: Discontinued
Link: https://www.spigotmc.org/resources/61280
Version: Version: 1.8  
Rating: Rating: 1  
Status: Discontinued

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

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