正则表达式获取降价文件中的部分
Regex get section in markdown file
我正在尝试使用正则表达式来获取 link 列表的每个部分 (https://raw.githubusercontent.com/Anything-Minecraft-Team/anything-minecraft/main/server/info/lists/plugins/anticheats.md)
我希望能够像这样从列表中获取每个部分
- [Anti Cheat](https://github.com/Paroxial/Anti-Cheat)
Version: 1.8
Rating: ???
Discontinued
所以我可以获得 version/rating 信息并知道 link 它来自什么
我有 (?:.*)(?:\n .*)*
选择在 above/below 文本之间有空格的文本(编辑不,我不是真的很笨)。但是由于我的格式,它不起作用,是否有其他方法可以做到这一点,或者我是否必须更改格式?
您可以获得 link 和使用 3 个捕获组的部分。
^- \[[^]\[]+]\((https?://[^\s()]+)\).*\R(Version:.*)\R(Rating:.*)\R(\S.+)$
^-
匹配字符串开头的 -
\[[^]\[]+]
匹配来自 [...]
\((https?://[^\s()]+)\)
匹配 (
并捕获组 1 中的 url 并匹配 )
.*\R
匹配该行的其余部分和一个换行符
(Version:.*)
抓包组2,匹配Version信息
\R(Rating:.*)
捕获组3,匹配Rating信息
\R(\S.+)$
匹配换行符并捕获单个空白字符和第 4 组中的其余行
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));
}
输出
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
我正在尝试使用正则表达式来获取 link 列表的每个部分 (https://raw.githubusercontent.com/Anything-Minecraft-Team/anything-minecraft/main/server/info/lists/plugins/anticheats.md)
我希望能够像这样从列表中获取每个部分
- [Anti Cheat](https://github.com/Paroxial/Anti-Cheat)
Version: 1.8
Rating: ???
Discontinued
所以我可以获得 version/rating 信息并知道 link 它来自什么
我有 (?:.*)(?:\n .*)*
选择在 above/below 文本之间有空格的文本(编辑不,我不是真的很笨)。但是由于我的格式,它不起作用,是否有其他方法可以做到这一点,或者我是否必须更改格式?
您可以获得 link 和使用 3 个捕获组的部分。
^- \[[^]\[]+]\((https?://[^\s()]+)\).*\R(Version:.*)\R(Rating:.*)\R(\S.+)$
^-
匹配字符串开头的-
\[[^]\[]+]
匹配来自[...]
\((https?://[^\s()]+)\)
匹配(
并捕获组 1 中的 url 并匹配)
.*\R
匹配该行的其余部分和一个换行符(Version:.*)
抓包组2,匹配Version信息\R(Rating:.*)
捕获组3,匹配Rating信息\R(\S.+)$
匹配换行符并捕获单个空白字符和第 4 组中的其余行
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));
}
输出
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