@Test public void matches_capture_group_nested_in_optional_one() { String regex = "^a (pre-commercial transaction |pre buyer fee model )?purchase(?: for \\$(\\d+))?$"; assertEquals(asList(null, null), match(compile(regex), "a purchase")); assertEquals(asList(null, 33), match(compile(regex), "a purchase for $33")); assertEquals(asList("pre buyer fee model ", null), match(compile(regex), "a pre buyer fee model purchase")); }
@Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); }
RegularExpression implements Expression { @Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); } }
RegularExpression implements Expression { @Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); } RegularExpression(Pattern expressionRegexp, ParameterTypeRegistry parameterTypeRegistry); }
RegularExpression implements Expression { @Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); } RegularExpression(Pattern expressionRegexp, ParameterTypeRegistry parameterTypeRegistry); @Override List<Argument<?>> match(String text, Type... typeHints); @Override Pattern getRegexp(); @Override String getSource(); }
RegularExpression implements Expression { @Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); } RegularExpression(Pattern expressionRegexp, ParameterTypeRegistry parameterTypeRegistry); @Override List<Argument<?>> match(String text, Type... typeHints); @Override Pattern getRegexp(); @Override String getSource();  }