JustDoom
JustDoom

Reputation: 109

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)

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). But due to my formatting it doesn't work, is there another way to do this or do I have to change the formatting?

Upvotes: 1

Views: 387

Answers (1)

The fourth bird
The fourth bird

Reputation: 163557

You can get the link and the sections using 3 capture groups.

^- \[[^]\[]+]\((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 )
  • .*\R Match the rest of the line and a newline
  • (Version:.*) Capture group 2, match the Version information
  • \R(Rating:.*) Capture group 3, match the Rating information
  • \R(\S.+)$ Match a newline and capture a single whitespace char and the rest of the line in group 4

Regex demo | Java demo

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

Upvotes: 1

Related Questions