Regular Expression in SoapUI Assertion: What It Checks and When to Use It
How regular expressions work in SoapUI assertions, why patterns must match the whole response, how to escape them, and when XPath, JsonPath or plain Contains is the better choice.
Checking a response with a regular expression in SoapUI is a common way to validate values whose exact content changes between runs, such as generated IDs, timestamps or status codes. The approach is flexible but has a few traps that cause assertions to fail or pass for the wrong reason. This review covers what a regex assertion actually evaluates, how to write patterns that behave predictably, and when a different assertion type is the better fit.
What a regex assertion actually checks
A regex assertion does not transform or rewrite the response. It reads the response as text and reports whether the pattern matches. Nothing in the stored message changes. The distinction matters because the response editor may show a formatted, indented view, while the assertion works on the message content as text. Whitespace, line breaks, namespace prefixes and attribute order are all part of what the pattern sees. A pattern written against the pretty-printed view can fail against the underlying text.
SoapUI runs on Java, so patterns follow Java regular expression syntax rather than JavaScript or PCRE conventions. Most everyday constructs are the same across these flavors. Some features differ, including certain lookbehind limitations, possessive quantifiers and inline flags. Check unfamiliar syntax against Java's rules before relying on it.
Where regular expressions appear in SoapUI
- Contains and Not Contains assertions: both have an option to treat the token as a regular expression instead of a literal string.
- JsonPath-based regex matching: SoapUI offers a JsonPath assertion that applies a regex to a value selected by a JsonPath expression. It is listed among the JsonPath assertion types, but availability depends on the SoapUI or ReadyAPI edition and version, so confirm it exists in your installation.
- Script assertions: a Groovy script can apply any regex to the response and combine it with other logic.
The whole-content matching rule
The most frequent surprise with the Contains assertion in regex mode is that the pattern must match the entire response, not just a fragment of it. This is the behavior of Java's full-string matching. A pattern like <status>OK</status> on its own will fail even when that element is present. The usual fix is to wrap the pattern so it can consume everything before and after the target. Add the (?s) flag so that the dot also matches line breaks:
(?s).*<status>OK</status>.*
Without (?s), the leading and trailing .* stop at the first line break. The assertion then fails on any multi-line response, which in practice means most SOAP and formatted JSON responses.
Example patterns
| Goal | Assertion | Pattern |
| Status element equals OK | Contains (regex) | (?s).<status>OK</status>. |
| Numeric orderId present in JSON | Contains (regex) | (?s)."orderId"\s:\s*\d+.* |
| No SOAP fault in the response | Not Contains (regex) | (?s).<(\w+:)?Fault>. |
| Date in YYYY-MM-DD form inside an element | Contains (regex) | (?s).<date>\d{4}-\d{2}-\d{2}</date>. |
Each pattern above follows the whole-content rule described earlier. The SOAP fault example allows an optional namespace prefix because services differ in the prefix they emit. These are illustrations of the syntax, not tested against a particular service. Run them against a real response before adopting them.
Escaping and property expansion
In the assertion's token field, write the regex exactly as the regex engine should see it. A digit class is typed as \d, not doubled as it would be inside a Java string literal. Characters with regex meaning need escaping when you want them literally. These include the dot, parentheses, brackets, question mark, plus, asterisk, pipe and dollar sign.
Property expansion such as ${#Project#expectedId} can be combined with a regex token, but the expanded value is inserted before the pattern is compiled. If the property contains characters like dots or parentheses, they become regex syntax. Java's \Q and \E markers quote everything between them literally:
(?s).*<id>\Q${#Project#expectedId}\E</id>.*
Using a Script assertion for finer control
When full-string matching gets awkward, a Groovy Script assertion can search for a fragment directly. Groovy's slashy string syntax also avoids double escaping. The =~ operator creates a matcher, and asserting on it succeeds if the pattern is found anywhere in the text:
def body = messageExchange.responseContent
assert body =~ /"orderId"\s*:\s*\d+/
def m = body =~ /"orderId"\s*:\s*(\d+)/
assert m.find()
assert m.group(1).toLong() > 0
The second form captures the value so it can be checked further or saved to a property. The cost is that the check is now code, which every team member must be able to read and maintain.
Is a regex assertion the right choice?
A regex assertion is a good fit when the value has a recognizable shape but no fixed content. Typical cases are formats, identifiers and codes, or responses that are not well-structured XML or JSON. Its main benefit is tolerance for values that change between runs. The deciding tradeoff is brittleness against structure. A regex treats XML or JSON as flat text, so harmless changes can break it. Examples include reordered attributes, a different namespace prefix, added whitespace or a field moving to another position. A regex can also falsely pass by matching the right text in the wrong element.
Prefer XPath Match or JsonPath Match when you are checking a specific field in a structured response. These assertions locate the field by its position in the document, not by surrounding text. A plain Contains assertion without regex is preferable when a literal string is enough. It is easier to read, needs no escaping and has no whole-content rule to remember. Reserve regex for the cases where a literal or path-based check cannot express the requirement.
Will patterns stay usable after copying or sharing?
Regex patterns are sensitive to characters that look identical on screen. Copying a pattern from a document, chat tool or web page can bring in curly quotes in place of straight quotes, non-breaking spaces, or invisible zero-width characters. Any of these makes the pattern silently stop matching. If a pasted pattern fails for no visible reason, retype the quotes and spaces by hand, or inspect the text in an editor that shows hidden characters.
Line endings are another portability issue. Responses from different servers may use different line-break conventions. A pattern that hard-codes a specific break sequence can work in one environment and fail in another. Using \s+ or relying on (?s) with .* avoids depending on the exact sequence.
For non-ASCII content, note that Java's case-insensitive matching covers only ASCII letters by default. Adding the inline flags (?iu) extends it to Unicode letters. SoapUI's Contains assertion also has its own ignore-case option. How that option interacts with regex mode was not confirmed for this review. If case handling matters, test it or use the inline flags explicitly.
SoapUI projects are saved as XML files, and the program escapes characters like < and & inside them. Patterns therefore survive saving and sharing through version control. Complex patterns are hard for colleagues to read. A short assertion name that states the intent, such as "orderId is numeric", makes shared projects easier to maintain.
Gaps in this review
No version-specific documentation or test results were available for this article. The guidance relies on Java regex behavior and generally documented SoapUI assertion features. Three points were not verified: exact differences between SoapUI open source and ReadyAPI, whether the regex assertions read raw or formatted response text in every case, and how the ignore-case option interacts with regex mode. Confirm these in your own installation before depending on them in a test suite.
Quick checklist
- Wrap Contains-regex patterns as (?s).pattern. so they match the whole response.
- Use Java regex syntax and single backslashes in the token field.
- Quote expanded property values with \Q and \E.
- Prefer XPath or JsonPath for specific fields in structured responses.
- Use plain Contains when a literal string is sufficient.
- Retype pasted quotes and spaces if a pattern fails unexpectedly.
- Test every pattern against a real response before relying on it.