Solved Matcher not finding section sign (§)

Discussion in 'Spigot Plugin Development' started by MrDienns, May 28, 2017.

  1. Hi,

    I'm using regex to find a section sign in a string (§) but it's not working. I Googled around a little and I did find a link to StackOverflow, but I couldn't really understand any of it.

    My current regex:
    Code (Text):
    /[^\u00A7][a-f0-9lnokmr]{1}/g
    I want to use it to filter out color code segments. It works fine when I test it with this simple example: https://regex101.com/r/zSPUIn/2/

    Any help would be nice :D
     
  2. Choco

    Moderator

    Since the section symbol can be system-dependent, it may be beneficial to concatenate ChatColor#COLOR_CHAR into that RegEx instead.
    Code (Java):
    "[^" + ChatColor.COLOR_CHAR + "][a-f0-9lnokmr]{1}"
     
    • Agree Agree x 1
  3. Thanks for the tip, doesn't make it work though.
     
    • Informative Informative x 1
  4. And also, you may only use lowercase for color codes but it's good to also account for uppercase too just to cover all cases. So that part would be [a-fA-Fk-oK-OrR0-9]
     
    • Like Like x 1
  5. There's a simple pattern in the ChatColor class.

    Code (Text):
    Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf(COLOR_CHAR) + "[0-9A-FK-OR]");
    You may also use ChatColor#stripColor(String) to strip all color codes from a given string.

    EDIT: Due to (?i), it's case insensitive!
     
    • Informative Informative x 1
  6. Oh, apparently there's also a freaking ChatColor#getLastColors method. That saves me some time LOL. Thanks! :D
     
  7. Choco

    Moderator

    I actually had no idea this was a thing in RegEx o: Good to keep in mind!