Visitar URL original
matchesRegex placeholder - regex with comma · Issue #306 · xmlunit/xmlunit · GitHub
Skip to content

matchesRegex placeholder - regex with comma #306

Open

Description

@pvgpe

Using a matchesRegex placeholder with a regex containing a comma (,) requires configuring a different placeholderArgsSeparatorRegex so the regex is not splitted in two arguments.

For example

<a>${xmlunit.matchesRegex(.{300,400})}</a>

would results in just .{300 used as a regex (which would fail to be parsed as a regex Pattern in this case)

This might not be obvious without inspecting the library code,
It also seems to me that it's mixing internal parsing of the placeholder depending on the argument value used.

Suggestion:

  • require quotes around arguments? would not be backwards compatible
  • make the arguments parsing / splitting placeholder-specific? e.g. matchesRegex would take the whole string and not split it?
  • document the behavior in https://github.com/xmlunit/user-guide/wiki/Placeholders / unit tests linked from there

Note I didn't test if there is a similar problem with ) inside the regex.

Related: wiremock/wiremock#3094

Please feel free to close if this is expected and the library used should use the proper constructor and configure the placeholderArgsSeparatorRegex

Activity

  1. bodewig commented on Jul 2, 2025

    @bodewig
    Member

    Your second suggestion would probably require a new method on the PlaceholderHandler interface that signalled who was responsible for parsing the argument list. So this would be backwards incompatible as well (xmlunit-placeholders like most of XMLUnit still targets Java 1.7 so we can't use a default method on the interface).

    Right now documenting the behavior explicitly seems the best was forward.

  2. DidierLoiseau commented on Sep 2, 2025

    @DidierLoiseau

    The situation is somewhat similar with (, because the default argsRegex will look for the last opening parenthesis. It then fails to match the keyword and thus fails to match.

    A simple workaround for this could be to just look for the first parenthesis, by starting the regex with (.*?)\s* instead (note the ? which makes it ungreedy). This might well cover most parenthesis issues without breaking anything.

    However for comas, I guess you could try to match opening and closing parentheses and [square] brackets, but it would probably be complicated and would still not cover the case of someone who would want to match just an opening or closing parenthesis.

    Maybe some kind of quoting/escaping would be possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions