Test regular expressions in SoapUI Contains assertions
How regular expressions work in SoapUI Contains assertions, with JSON and XML pattern examples, testing steps and the cases where XPath, JSONPath or plain text matching fits better.
SoapUI's Contains assertion checks whether a response includes some expected content. A Regular Expression option changes it from a literal text search to a pattern match. This review covers what that option changes, how to write patterns that behave as intended, and when a different assertion is the better choice. No version-specific documentation or test results were supplied for this article. Treat the behavior described here as general guidance and confirm it in your own SoapUI installation.
Is a regex Contains assertion right for your test?
A regex Contains assertion works well when you need to confirm that a value with a known format appears somewhere in a response. Examples include an order ID, a date or a status word, especially when the exact value changes between runs. Its main benefit is that you can check format without hard-coding values. The main tradeoff is that it matches raw text and ignores structure, so it cannot tell which element or field a match came from. If the expected value is fixed, a plain Contains assertion with the Regular Expression option turned off is simpler and harder to get wrong. If the position of a value in the XML or JSON structure matters, an XPath Match or JSONPath assertion is the better tool.
What the Regular Expression option changes
The option does not modify the response. It changes how SoapUI reads the text in the assertion's Content field. With the option off, the content is a literal string, and the assertion passes if that string appears anywhere in the response. With the option on, the content is a Java-flavored regular expression. SoapUI documentation and community examples commonly describe the pattern as needing to match the entire response rather than just a substring. That is why regex Contains patterns are usually wrapped in a leading and trailing .* along with a flag that lets the dot cross line breaks. Despite the name, a regex Contains assertion behaves more like a whole-text match, so write patterns with that in mind. If your version behaves differently, the wrapped form still works, because .* also matches an empty string.
(?s).*Success.*
In this pattern, (?s) turns on Java's DOTALL mode, so the dot also matches newline characters. Without it, .* stops at the first line break, and a pattern can fail against a pretty-printed or multi-line response even though the target text is present. The leading and trailing .* allow any text before and after the part you care about.
Worked examples
Suppose a REST call returns a JSON body like the one below. You want to confirm that an order ID is present and has the format of the letter A followed by five digits, without fixing the exact number.
{
"status": "OK",
"orderId": "A12345"
}
(?s).*"orderId"\s*:\s*"A\d{5}".*
Here is how the pattern matches the sample:
• (?s).* consumes the opening brace and the status line.
• "orderId" matches the key literally.
• \s*:\s* allows optional whitespace around the colon, so the pattern still works whether or not the response is compact or pretty-printed.
• "A\d{5}" matches "A12345" exactly: one letter A, then five digits, then the closing quote.
• The final .* covers the rest of the body.
An ID such as A1234 or B12345 would make the assertion fail. That is the intended behavior. Type the pattern into SoapUI with single backslashes as shown. The Content field takes the regular expression itself, not a Java string literal, so do not double the backslashes.
For a SOAP response, the same approach applies to XML text. The following pattern checks for a Status element whose value is Success, whatever namespace prefix the service uses.
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns:Result>
<ns:Status>Success</ns:Status>
</ns:Result>
</soap:Body>
</soap:Envelope>
(?s).*<\w+:Status>Success</\w+:Status>.*
Against the sample, \w+ matches the prefix ns, and the literal tags and value match as written. This pattern has blind spots. It does not match an unprefixed Status element. It also fails if the element carries attributes or has whitespace around the value. And it cannot confirm that this Status is the one inside Result rather than another element elsewhere in the response. If any of those cases matter, use an XPath Match assertion instead.
| Goal | Pattern | Note |
| Text appears anywhere in a multi-line response | (?s).Success. | A plain Contains without regex does the same job more simply |
| Case-insensitive match | (?is).success. | Inline flags can be combined |
| Date-formatted value in a JSON field | (?s)."createdAt"\s:\s*"\d{4}-\d{2}-\d{2}.* | Checks the format only, not whether the date is valid |
| No SOAP fault present | (?s).<faultstring>. | Use with a Not Contains assertion; a prefixed or differently named fault element would not match |
Testing that the assertion can actually fail
- Add a Contains assertion to the test step, enter the pattern, and turn on the Regular Expression option.
- Run the step against a known-good response and confirm that the assertion passes.
- Change one significant part of the pattern, such as a digit count or a literal word, and rerun. The assertion should now fail.
- Restore the pattern. If the assertion passed in both runs, the pattern is too broad. A common cause is a stray .* that can match anything.
- Check the raw response text, not just a formatted view, when you work out line breaks and whitespace.
Will a pattern survive copying and sharing?
Patterns copied from other regex tools, documents or chat messages can break without any visible sign:
• Regex flavors differ. A pattern built in a tester set to another engine may use syntax that Java handles differently or does not support. Select a Java flavor when testing elsewhere, or test inside SoapUI.
• Word processors and some chat apps may convert straight quotes to curly quotes or insert non-breaking spaces. These look the same on screen but do not match the plain characters in a response.
• Responses may use carriage return plus line feed line endings. A pattern that spells out exact line breaks can fail where one using (?s).* or \s+ succeeds.
When you share a pattern with a team, include a plain-text sample response it is meant to match. That way others can check that the pattern still works after it has been pasted.
Unicode content needs extra care. By default, Java's \w, \d and \b classes recognize only ASCII characters. As a result, a pattern like \w+ will not match accented letters or non-Latin scripts in a response. The (?U) inline flag enables Unicode-aware character classes in Java. How SoapUI's own Ignore Case checkbox handles non-ASCII text was not covered by the supplied evidence. If case-insensitive matching on non-ASCII content matters, test it directly rather than assuming it works.
When another approach fits better
- Fixed, known value: use a plain Contains assertion with the Regular Expression option turned off.
- Value must sit at a specific location in XML: use XPath Match.
- Value must sit at a specific location in JSON: use a JSONPath-based assertion, if your SoapUI edition provides one.
- Several related checks, or a regex that needs substring matching rather than whole-text matching: use a Script assertion in Groovy.
def content = messageExchange.responseContent
assert content =~ /"orderId"\s*:\s*"A\d{5}"/
In this Groovy script assertion, the =~ operator creates a matcher. When used as a condition, it is true if the pattern is found anywhere in the text, so you don't need the (?s).* wrapping. The slashy string syntax also means backslashes are not doubled. The tradeoff is that the assertion becomes code that needs maintenance and is harder for non-programmers to review.
Gaps in the available evidence
No release notes, version numbers or scoped test results were provided for this review. The whole-response matching behavior described above reflects widely shared SoapUI guidance and Java regex semantics, not a verified test on a specific version. Also unconfirmed are the exact implementation of the Ignore Case option and whether the open-source and commercial editions differ. Before you rely on these points in a test suite, confirm them with the pass-then-fail check described earlier.