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

The following examples show how to use java.util.regex.Pattern#pattern() . These examples are extracted from open source projects. 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
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
@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
public String getNoKeepAliveUserAgents() {
    Pattern p = noKeepAliveUserAgents;
    if (p == null) {
        return null;
    } else {
        return p.pattern();
    }
}
 
Example 4
Source Project: cucumber   File: TreeRegexp.java    License: 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 5
Source Project: tomcatsrc   File: SpnegoAuthenticator.java    License: 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 6
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 7
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 8
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 9
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 10
Source Project: K-Sonic   File: HlsPlaylistParser.java    License: 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 11
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 12
/**
 * 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 13
@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 14
@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 15
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);
}
 
Example 16
@Nullable
static String patternToString(@Nullable Pattern p) {
    return p == null ? null : p.pattern();
}
 
Example 17
public UrlFilteredHarEntriesSupplier(Har har, Pattern pattern) {
    super(har, new AssertionUrlFilterInfo(pattern.pattern()));
    this.pattern = pattern;
}
 
Example 18
@Override
public JsonElement serialize(Pattern src) {
    return new JsonPrimitive(src.pattern());
}
 
Example 19
Source Project: lams   File: PatternEditor.java    License: 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 20
Source Project: consulo   File: UsageViewPresentation.java    License: 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();
}