Java Code Examples for com.helger.commons.regex.RegExHelper#stringMatchesPattern()

The following examples show how to use com.helger.commons.regex.RegExHelper#stringMatchesPattern() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocaleHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String getValidCountryCode (@Nullable final String sCode)
{
  // Allow for 2 or 3 letter codes ("AT" and "AUT")
  if (StringHelper.hasText (sCode))
  {
    // Allow for 2 letter codes ("AT")
    if (RegExHelper.stringMatchesPattern ("[a-zA-Z]{2}|[0-9]{3}", sCode))
    {
      return sCode.toUpperCase (CGlobal.LOCALE_FIXED_NUMBER_FORMAT);
    }

    // Allow for 3 letter codes ("AUT")
    if (RegExHelper.stringMatchesPattern ("[a-zA-Z]{3}", sCode))
    {
      final String sAlpha3 = sCode.toUpperCase (CGlobal.LOCALE_FIXED_NUMBER_FORMAT);
      final String sAlpha2 = COUNTRY_ISO3TO2.get (sAlpha3);
      return sAlpha2 != null ? sAlpha2 : sAlpha3;
    }
  }
  return null;
}
 
Example 2
Source File: AbstractReadOnlyMapBasedMultilingualText.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
private static void _performConsistencyChecks (@Nonnull final String sValue)
{
  // String contains masked newline? warning only!
  if (sValue.contains ("\\n"))
    if (LOGGER.isWarnEnabled ())
      LOGGER.warn ("Passed string contains a masked newline - replace with an inline one:\n" + sValue);

  if (sValue.contains ("{0}"))
  {
    // When formatting is used, 2 single quotes are required!
    if (RegExHelper.stringMatchesPattern ("^'[^'].*", sValue))
      throw new IllegalArgumentException ("The passed string seems to start with unclosed single quotes: " + sValue);
    if (RegExHelper.stringMatchesPattern (".*[^']'[^'].*", sValue))
      throw new IllegalArgumentException ("The passed string seems to contain unclosed single quotes: " + sValue);
  }
  else
  {
    // When no formatting is used, single quotes are required!
    if (RegExHelper.stringMatchesPattern (".*''.*", sValue))
      throw new IllegalArgumentException ("The passed string seems to contain 2 single quotes: " + sValue);
  }
}
 
Example 3
Source File: IFileFilter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Create a file filter that matches, if it matches one of the provided
 * regular expressions
 *
 * @param aRegExs
 *        The regular expressions to match against. May neither be
 *        <code>null</code> nor empty.
 * @return The created {@link IFileFilter}. Never <code>null</code>.
 * @see #filenameMatchNoRegEx(String...)
 * @see #filenameMatchAny(String...)
 * @see #filenameMatchNone(String...)
 */
@Nonnull
static IFileFilter filenameMatchAnyRegEx (@Nonnull @Nonempty final String... aRegExs)
{
  ValueEnforcer.notEmpty (aRegExs, "RegularExpressions");
  return aFile -> {
    if (aFile != null)
    {
      final String sRealName = FilenameHelper.getSecureFilename (aFile.getName ());
      if (sRealName != null)
        for (final String sRegEx : aRegExs)
          if (RegExHelper.stringMatchesPattern (sRegEx, sRealName))
            return true;
    }
    return false;
  };
}
 
Example 4
Source File: IFileFilter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Create a file filter that matches, if it matches none of the provided
 * regular expressions
 *
 * @param aRegExs
 *        The regular expressions to match against. May neither be
 *        <code>null</code> nor empty.
 * @return The created {@link IFileFilter}. Never <code>null</code>.
 * @see #filenameMatchAnyRegEx(String...)
 * @see #filenameMatchAny(String...)
 * @see #filenameMatchNone(String...)
 */
@Nonnull
static IFileFilter filenameMatchNoRegEx (@Nonnull @Nonempty final String... aRegExs)
{
  ValueEnforcer.notEmpty (aRegExs, "RegularExpressions");
  return aFile -> {
    if (aFile == null)
      return false;
    final String sRealName = FilenameHelper.getSecureFilename (aFile.getName ());
    if (sRealName == null)
      return false;
    for (final String sRegEx : aRegExs)
      if (RegExHelper.stringMatchesPattern (sRegEx, sRealName))
        return false;
    return true;
  };
}
 
Example 5
Source File: CSSRectHelper.java    From ph-css with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the passed value is CSS rectangle definition or not. It checks
 * both the current syntax (<code>rect(a,b,c,d)</code>) and the old syntax (
 * <code>rect(a b c d)</code>).
 *
 * @param sCSSValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if the passed value is a rect value,
 *         <code>false</code> if not
 */
public static boolean isRectValue (@Nullable final String sCSSValue)
{
  final String sRealValue = StringHelper.trim (sCSSValue);
  if (StringHelper.hasText (sRealValue))
  {
    // Current syntax: rect(a,b,c,d)
    if (RegExHelper.stringMatchesPattern (PATTERN_CURRENT_SYNTAX, sRealValue))
      return true;

    // Backward compatible syntax: rect(a b c d)
    if (RegExHelper.stringMatchesPattern (PATTERN_OLD_SYNTAX, sRealValue))
      return true;
  }
  return false;
}
 
Example 6
Source File: MainFindMaskedXMLChars.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private static boolean _containsER (final String s, final int c)
{
  if (s.indexOf ("&#" + c + ";") >= 0)
    return true;
  if (s.indexOf ("&#x" + Integer.toString (c, 16) + ";") >= 0)
    return true;
  return RegExHelper.stringMatchesPattern (".+&[a-z]+;.+", s);
}
 
Example 7
Source File: LocaleHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String getValidLanguageCode (@Nullable final String sCode)
{
  if (StringHelper.hasText (sCode) &&
      (RegExHelper.stringMatchesPattern ("[a-zA-Z]{2,8}", sCode) || isSpecialLocaleCode (sCode)))
  {
    return sCode.toLowerCase (CGlobal.LOCALE_FIXED_NUMBER_FORMAT);
  }
  return null;
}
 
Example 8
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Check if the passed String is valid CSS RGB color value. Example value:
 * <code>rgb(255,0,0)</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS RGB color value,
 *         <code>false</code> if not
 */
public static boolean isRGBColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  // startsWith as speedup
  return StringHelper.hasText (sRealValue) &&
         sRealValue.startsWith (CCSSValue.PREFIX_RGB) &&
         RegExHelper.stringMatchesPattern (PATTERN_RGB, sRealValue);
}
 
Example 9
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Check if the passed String is valid CSS RGBA color value. Example value:
 * <code>rgba(255,0,0, 0.1)</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS RGBA color value,
 *         <code>false</code> if not
 */
public static boolean isRGBAColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  return StringHelper.hasText (sRealValue) &&
         sRealValue.startsWith (CCSSValue.PREFIX_RGBA) &&
         RegExHelper.stringMatchesPattern (PATTERN_RGBA, sRealValue);
}
 
Example 10
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Check if the passed String is valid CSS HSL color value. Example value:
 * <code>hsl(255,0%,0%)</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS HSL color value,
 *         <code>false</code> if not
 */
public static boolean isHSLColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  return StringHelper.hasText (sRealValue) &&
         sRealValue.startsWith (CCSSValue.PREFIX_HSL) &&
         RegExHelper.stringMatchesPattern (PATTERN_HSL, sRealValue);
}
 
Example 11
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Check if the passed String is valid CSS HSLA color value. Example value:
 * <code>hsla(255,0%,0%, 0.1)</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS HSLA color value,
 *         <code>false</code> if not
 */
public static boolean isHSLAColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  return StringHelper.hasText (sRealValue) &&
         sRealValue.startsWith (CCSSValue.PREFIX_HSLA) &&
         RegExHelper.stringMatchesPattern (PATTERN_HSLA, sRealValue);
}
 
Example 12
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Check if the passed String is valid CSS hex color value. Example value:
 * <code>#ff0000</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS hex color value,
 *         <code>false</code> if not
 */
public static boolean isHexColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  return StringHelper.hasText (sRealValue) &&
         sRealValue.charAt (0) == CCSSValue.PREFIX_HEX &&
         RegExHelper.stringMatchesPattern (PATTERN_HEX, sRealValue);
}