Java Code Examples for java.util.regex.Pattern#pattern()

The following examples show how to use java.util.regex.Pattern#pattern() . 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: UpdateDocExtensionsListMojo.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
static void replace(AtomicReference<String> ref, Path documentPath, String list, org.apache.camel.catalog.Kind kind) {
    final Pattern pat = Pattern.compile("(" + Pattern.quote("// " + kind.name() + "s: START\n") + ")(.*)("
            + Pattern.quote("// " + kind.name() + "s: END\n") + ")", Pattern.DOTALL);

    final String document = ref.get();
    final Matcher m = pat.matcher(document);

    final StringBuffer sb = new StringBuffer(document.length());
    if (m.find()) {
        m.appendReplacement(sb, "$1" + Matcher.quoteReplacement(list) + "$3");
    } else {
        throw new IllegalStateException("Could not find " + pat.pattern() + " in " + documentPath + ":\n\n" + document);
    }
    m.appendTail(sb);
    ref.set(sb.toString());
}
 
Example 2
Source File: XmlFileNameValidator.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override public boolean isValid(GraphRewrite event,EvaluationContext context, XmlFileModel model)
{
    if (fileNamePattern != null)
    {
        final ParameterStore store = DefaultParameterStore.getInstance(context);
        Pattern compiledPattern = fileNamePattern.getCompiledPattern(store);
        String pattern = compiledPattern.pattern();
        String fileName = model.getFileName();
        if (!fileName.matches(pattern))
        {
            return false;
        }
        return true;
    }
    return true;
}
 
Example 3
Source File: RegexPatternsPasswordPolicyProvider.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public PolicyError validate(String username, String password) {
    Pattern pattern = context.getRealm().getPasswordPolicy().getPolicyConfig(RegexPatternsPasswordPolicyProviderFactory.ID);
    Matcher matcher = pattern.matcher(password);
    if (!matcher.matches()) {
        return new PolicyError(ERROR_MESSAGE, pattern.pattern());
    }
    return null;
}
 
Example 4
Source File: CatalogObjectValidator.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public String validate(String parameterValue, ModelValidatorContext context) throws ValidationException {
    Pattern pattern = Pattern.compile(CATALOG_OBJECT_MODEL_REGEXP);
    Matcher matcher = pattern.matcher(parameterValue);
    if (!(parameterValue.matches(CATALOG_OBJECT_MODEL_REGEXP) && matcher.find())) {
        throw new ValidationException("Expected value should match regular expression " + pattern.pattern() +
                                      " , received " + parameterValue);
    }
    return parameterValue;
}
 
Example 5
Source File: TreeRegexp.java    From cucumber with MIT License 5 votes vote down vote up
private static GroupBuilder createGroupBuilder(Pattern pattern) {
    String source = pattern.pattern();
    Deque<GroupBuilder> stack = new ArrayDeque<>(singleton(new GroupBuilder()));
    Deque<Integer> groupStartStack = new ArrayDeque<>();
    boolean escaping = false;
    boolean charClass = false;

    for (int i = 0; i < source.length(); i++) {
        char c = source.charAt(i);
        if (c == '[' && !escaping) {
            charClass = true;
        } else if (c == ']' && !escaping) {
            charClass = false;
        } else if (c == '(' && !escaping && !charClass) {
            groupStartStack.push(i);
            boolean nonCapturing = isNonCapturingGroup(source, i);
            GroupBuilder groupBuilder = new GroupBuilder();
            if (nonCapturing) {
                groupBuilder.setNonCapturing();
            }
            stack.push(groupBuilder);
        } else if (c == ')' && !escaping && !charClass) {
            GroupBuilder gb = stack.pop();
            int groupStart = groupStartStack.pop();
            if (gb.isCapturing()) {
                gb.setSource(source.substring(groupStart + 1, i));
                stack.peek().add(gb);
            } else {
                gb.moveChildrenTo(stack.peek());
            }
        }
        escaping = c == '\\' && !escaping;
    }
    return stack.pop();
}
 
Example 6
Source File: SpnegoAuthenticator.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public String getNoKeepAliveUserAgents() {
    Pattern p = noKeepAliveUserAgents;
    if (p == null) {
        return null;
    } else {
        return p.pattern();
    }
}
 
Example 7
Source File: HlsPlaylistParser.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private static String parseStringAttr(
    String line, Pattern pattern, Map<String, String> variableDefinitions)
    throws ParserException {
  String value = parseOptionalStringAttr(line, pattern, variableDefinitions);
  if (value != null) {
    return value;
  } else {
    throw new ParserException("Couldn't match " + pattern.pattern() + " in " + line);
  }
}
 
Example 8
Source File: PatternValueDslProperty.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
protected T createAndValidateProperty(Pattern pattern, Object object) {
	if (object != null) {
		String generatedValue = object.toString();
		boolean matches = pattern.matcher(generatedValue).matches();
		if (!matches) {
			throw new IllegalStateException("The generated value [" + generatedValue
					+ "] doesn\'t match the pattern [" + pattern.pattern() + "]");
		}

		return createProperty(pattern, object);
	}

	return createProperty(pattern, object);
}
 
Example 9
Source File: SpnegoAuthenticator.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public String getNoKeepAliveUserAgents() {
    Pattern p = noKeepAliveUserAgents;
    if (p == null) {
        return null;
    } else {
        return p.pattern();
    }
}
 
Example 10
Source File: HlsPlaylistParser.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static String parseStringAttr(String line, Pattern pattern) throws ParserException {
  Matcher matcher = pattern.matcher(line);
  if (matcher.find() && matcher.groupCount() == 1) {
    return matcher.group(1);
  }
  throw new ParserException("Couldn't match " + pattern.pattern() + " in " + line);
}
 
Example 11
Source File: HlsPlaylistParser.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private static String parseStringAttr(
    String line, Pattern pattern, Map<String, String> variableDefinitions)
    throws ParserException {
  String value = parseOptionalStringAttr(line, pattern, variableDefinitions);
  if (value != null) {
    return value;
  } else {
    throw new ParserException("Couldn't match " + pattern.pattern() + " in " + line);
  }
}
 
Example 12
Source File: HlsPlaylistParser.java    From K-Sonic with MIT License 5 votes vote down vote up
private static String parseStringAttr(String line, Pattern pattern) throws ParserException {
  Matcher matcher = pattern.matcher(line);
  if (matcher.find() && matcher.groupCount() == 1) {
    return matcher.group(1);
  }
  throw new ParserException("Couldn't match " + pattern.pattern() + " in " + line);
}
 
Example 13
Source File: TextFile.java    From systemsgenetics with GNU General Public License v3.0 5 votes vote down vote up
public void writelnDelimited(Object[] vals, Pattern p) throws IOException {
	String delim = "";
	for (Object val : vals) {
		out.write(delim);
		out.write(val.toString());
		delim = p.pattern();
	}
	writeln();
}
 
Example 14
Source File: CellFormatPart.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the number of the first group that is the same as the marker
 * string. Starts from group 1.
 *
 * @param pat    The pattern to use.
 * @param str    The string to match against the pattern.
 * @param marker The marker value to find the group of.
 *
 * @return The matching group number.
 *
 * @throws IllegalArgumentException No group matches the marker.
 */
private static int findGroup(Pattern pat, String str, String marker) {
    Matcher m = pat.matcher(str);
    if (!m.find())
        throw new IllegalArgumentException(
                "Pattern \"" + pat.pattern() + "\" doesn't match \"" + str +
                        "\"");
    for (int i = 1; i <= m.groupCount(); i++) {
        String grp = m.group(i);
        if (grp != null && grp.equals(marker))
            return i;
    }
    throw new IllegalArgumentException(
            "\"" + marker + "\" not found in \"" + pat.pattern() + "\"");
}
 
Example 15
Source File: PatternAdapter.java    From Hyperium with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public JsonElement serialize(Pattern src) {
    return new JsonPrimitive(src.pattern());
}
 
Example 16
Source File: UsageViewPresentation.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static int getHashCode(Pattern pattern) {
  if (pattern == null) return 0;
  String s = pattern.pattern();
  return (s != null ? s.hashCode() : 0) * 31 + pattern.flags();
}
 
Example 17
Source File: PatternEditor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getAsText() {
	Pattern value = (Pattern) getValue();
	return (value != null ? value.pattern() : "");
}
 
Example 18
Source File: UrlFilteredHarEntriesSupplier.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public UrlFilteredHarEntriesSupplier(Har har, Pattern pattern) {
    super(har, new AssertionUrlFilterInfo(pattern.pattern()));
    this.pattern = pattern;
}
 
Example 19
Source File: BluetoothDeviceFilterUtils.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Nullable
static String patternToString(@Nullable Pattern p) {
    return p == null ? null : p.pattern();
}
 
Example 20
Source File: PopupMenuEvidence.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
private void addMenuItem(final Pattern pattern, final ExtensionSearch.Type type) {
    JMenuItem menuItem = new JMenuItem(pattern.pattern());
    menuItem.addActionListener(e -> extension.search(pattern, type));
    this.add(menuItem);
}