Class RegexTools

java.lang.Object
org.vaadin.miki.util.RegexTools

public class RegexTools extends Object
Tools related to regular expressions.
Since:
2023-05-05
Author:
miki
  • Field Details

    • CHARACTERS_TO_ESCAPE_OUTSIDE_CHAR_SELECTOR

      public static final Set<Character> CHARACTERS_TO_ESCAPE_OUTSIDE_CHAR_SELECTOR
      Characters that need to be escaped in a regular expression.
    • CHARACTERS_TO_ESCAPE_ONLY_IN_CHAR_SELECTOR

      public static final Set<Character> CHARACTERS_TO_ESCAPE_ONLY_IN_CHAR_SELECTOR
  • Method Details

    • escaped

      public static String escaped(char character)
      Escapes the character if it needs to in a regular expression that is part of a character selector. This is the same as calling escaped(character, true).
      Parameters:
      character - Character to escape.
      Returns:
      Escaped character (if needed), otherwise the passed character.
      See Also:
    • escaped

      public static String escaped(char character, boolean insideCharSelector)
      Escapes the character if it needs to in a regular expression.
      Parameters:
      character - Character to escape.
      insideCharSelector - If true, CHARACTERS_TO_ESCAPE_ONLY_IN_CHAR_SELECTOR will be used to check if a character needs to be escaped; otherwise CHARACTERS_TO_ESCAPE_OUTSIDE_CHAR_SELECTOR will be used.
      Returns:
      Escaped character (if needed), otherwise the passed character.
    • characterSelector

      public static StringBuilder characterSelector(StringBuilder builder, char mainCharacter, Collection<Character> alternatives)
      Appends to a given builder a regular expression that is a selector for a given character(s).
      Parameters:
      builder - Builder to add the regular expression to.
      mainCharacter - The character to select.
      alternatives - Eventual alternatives.
      Returns:
      The passed builder.
    • characterSelector

      public static String characterSelector(char mainCharacter, Collection<Character> alternatives)
      Builds a regular expression that is a selector for a given character(s).
      Parameters:
      mainCharacter - The character to select.
      alternatives - Eventual alternatives.
      Returns:
      A regular expression that matches the character or its alternatives.
    • characterSelector

      public static String characterSelector(char main, char... alternatives)
      Builds a regular expression that is a selector for a given character(s).
      Parameters:
      main - The character to select.
      alternatives - Eventual alternatives.
      Returns:
      A regular expression that matches the character or its alternatives.