# Regular Expressions

Regular expressions are text patterns that describe character sequences for searching, matching, extracting, or replacing data.

Cloud concept. Updated 2026-10-29. Page: https://blowstack.com/tools/cloud-concepts-hub/regular-expressions

> Regular expressions let engineers recognize variable text without enumerating every possible value. In cloud operations, they help locate failures, extract fields from unstructured logs, and select events for monitoring or forwarding, but their syntax and capabilities depend on the system evaluating them.

Practice exams covering Regular Expressions: [DVA-C02 AWS Certified Developer - Associate](https://blowstack.com/practice-exams/aws-certified-developer-associate), [DOP-C02 AWS Certified DevOps Engineer - Professional](https://blowstack.com/practice-exams/aws-certified-devops-engineer-professional).

## 6 key aspects of Regular Expressions

Reliable regex use combines precise matching, appropriate extraction, engine-aware syntax, and testing against real data and execution constraints.

1. **Pattern-based matching**: Combine literals, character classes, alternation, and quantifiers to recognize variable text. Include field delimiters or boundaries when a substring match would accept invalid values.
2. **Anchors and match scope**: Use anchors when the entire evaluated string must follow a format. Confirm whether the target operation searches for a substring or requires a full match, and whether multiline behavior changes anchor semantics.
3. **Extraction as well as filtering**: Matching selects records, while capture groups can turn unstructured message fragments into fields. Extracted latency, request identifiers, or error categories can then support aggregation and investigation.
4. **Engine-specific syntax**: Regex support varies across services and operations, including grouping, flags, character handling, and escaping. CloudWatch Logs filter patterns and Logs Insights queries use different delimiters and support different capabilities.
5. **Structured data first**: Use native field comparisons for structured JSON whenever practical. Reserve regex for variable text or fields whose contents require pattern matching rather than simple equality or numeric comparison.
6. **Correctness and execution cost**: Test positive, negative, and malformed examples to detect missed events and false positives. Constrain query scope and avoid unnecessarily complex patterns, especially in engines susceptible to expensive backtracking.

## Cloud services for Regular Expressions

CloudWatch Logs can collect operational logs, apply incoming-event filters, and support immediate investigation through Logs Insights. Logs exported or delivered to Amazon S3 can support longer-range SQL analysis with Athena, where regex functions extract or classify text. Logs indexed in Amazon OpenSearch Service support search-oriented investigations using its own regex semantics. Across these paths, preserve structured fields where possible, validate patterns separately for each engine, and choose retention, delivery, partitioning, and indexing settings independently of the regex itself.

- **[Amazon Athena](https://blowstack.com/tools/cloud-services-hub/amazon-athena)**: Provides SQL regex functions such as regexp_like and regexp_extract for analyzing data, including logs stored in Amazon S3. Partition pruning and appropriate storage formats remain important because applying a regex does not inherently avoid scanning data.
- **[Amazon OpenSearch Service](https://blowstack.com/tools/cloud-services-hub/amazon-opensearch-service)**: Supports regular-expression queries against indexed terms using Lucene regex syntax, which differs from common application regex engines. Field mappings and keyword subfields matter because term-level regex queries do not simply search the original complete log message.

## Regular Expressions exam questions

Real practice exam questions on Regular Expressions, with the correct answer and its explanation.

### Question 1 (DVA-C02)

**Question:**

A developer is investigating TLS negotiation failures in an application's Amazon CloudWatch Logs. Each log event consists of key=value tokens separated by single spaces and contains exactly one errorCode token. The token can appear anywhere in the event. For example:

level=ERROR errorCode=TLS.12 host=api.internal
errorCode=tls.19 level=ERROR host=auth.internal
level=WARN errorCode=TLSX12 host=api.internal
level=ERROR errorCode=TLS.120 host=auth.internal

The developer uses CloudWatch Logs Insights and selects the incident's complete time range. The developer needs only events whose complete errorCode value is TLS.10 through TLS.19, regardless of letter case. Values such as TLSX12 and TLS.120 must not match.

Which filter clause meets these requirements?

**Correct answer:** filter @message like /(?i)(^| )errorCode=TLS\.1[0-9]( |$)/

**Explanation:** The (?i) modifier makes matching case-insensitive. The escaped dot requires a literal period, and 1[0-9] matches exactly 10 through 19. The surrounding space-or-event-boundary checks require a complete errorCode token, excluding values such as TLS.120.

### Question 2 (DOP-C02)

**Question:**

A company stores plaintext authorization logs in an Amazon CloudWatch Logs Standard log group. Events include the following examples:

principal="arn:aws:sts::111122223333:assumed-role/Deploy-Prod/build-841" outcome=AccessDenied
principal="arn:aws:sts::111122223333:assumed-role/Deploy-Prod/build-842" outcome=AccessDenied
principal="arn:aws:sts::444455556666:assumed-role/ReadOnly/audit-17" outcome=Allowed
principal="arn:aws:iam::111122223333:user/operator" outcome=AccessDenied

Every authorization event follows the shown principal="<ARN>" outcome=<value> format. The log group also contains unrelated diagnostic events. Assumed-role ARNs use the aws partition and contain a role name followed by a session name. Role names can contain letters, digits, underscores, and hyphens.

A DevOps engineer must query the existing logs to count AccessDenied events for assumed-role principals, grouped by AWS account ID and role name. Different sessions of the same role must contribute to the same group. The results must exclude allowed requests, IAM user principals, and unrelated events, including any group with missing account or role values.

Which CloudWatch Logs Insights query meets these requirements?

**Correct answer:** parse @message /^principal="arn:aws:sts::(?<accountId>[0-9]{12}):assumed-role\/(?<roleName>[^\/"]+)\/[^\"]+" outcome=AccessDenied$/
| filter ispresent(accountId) and ispresent(roleName)
| stats count(*) as deniedRequests by accountId, roleName

**Explanation:** The named captures extract the account ID and only the role-name segment. The role capture cannot consume a slash, so session names are excluded from the grouping key. The full expression matches only assumed-role AccessDenied events. Because parse retains nonmatching events without populating the extracted fields, the ispresent filter removes those events before aggregation.

## Practice Regular Expressions in a full exam

Questions on Regular Expressions in each practice exam, with every answer option and an explanation for each:

- [DOP-C02](https://blowstack.com/practice-exams/aws-certified-devops-engineer-professional): 6 questions
- [DVA-C02](https://blowstack.com/practice-exams/aws-certified-developer-associate): 1 question

## What is Regular Expressions

A regular expression represents a pattern rather than one fixed string. Literal characters match themselves, character classes such as \[0-9\] match a range, and quantifiers control repetition. For example, requestId=\[A-Za-z0-9-\]+ matches a request identifier containing letters, digits, or hyphens. Anchors such as ^ and $ constrain matches to the beginning or end of the evaluated text. Without anchors, many search functions accept a matching substring rather than requiring the entire message to match.

Cloud engineers use these patterns to investigate application failures, identify suspicious requests, extract latency values, and select logs for downstream processing. A useful pattern should reflect the actual log format. For example, status=5\[0-9\]{2} identifies a status field beginning with a three-digit server-error code, but could also match the beginning of a longer number unless an appropriate delimiter or boundary is required. Test representative successes, failures, missing fields, and malformed messages before relying on a pattern for alerting.

In Amazon CloudWatch Logs, distinguish filter patterns from CloudWatch Logs Insights queries. Filter patterns support searching log events and configuring metric filters and subscription filters. They include syntax for unstructured text, JSON, and space-delimited records; regular expressions are only one part of that language. Supported regex expressions are enclosed in percent signs, such as %ERROR|Timeout%. This regex dialect is restricted: parentheses and multibyte characters are not supported, and quotas limit regex use within filter patterns and log groups. A pattern copied from an application regex library might therefore be rejected.

CloudWatch Logs Insights uses a separate query language. For example, fields @timestamp, @message | filter @message like /ERROR|Timeout/ | sort @timestamp desc | limit 50 searches stored events for either term. Matching is case-sensitive by default; supported inline flags such as (?i) allow case-insensitive matching. The parse command can extract values using regular expressions: parse @message /latency=(?<latencyMs>\[0-9\]+)/ | stats avg(latencyMs) by bin(5m) extracts a numeric latency field and calculates five-minute averages. The parse command also supports glob patterns, which are not regular expressions.

Prefer structured fields when they already exist. A Logs Insights condition such as filter status >= 500 and status < 600 is clearer than searching serialized JSON with a regex, provided status is a numeric field. Similarly, a JSON metric-filter pattern such as { $.status >= 500 } uses structured comparison syntax, not regex syntax. Metric filters generate metrics from newly ingested matching events; they do not retroactively calculate metrics from historical logs. Subscription filters select incoming events for delivery without changing the source log group's retention policy.

Regex precision affects correctness and operational cost. Broad expressions can produce false positives, while overly strict expressions can silently miss changed log formats. Greedy repetition can capture more text than intended, and some backtracking engines can consume excessive CPU on pathological patterns; engine implementations differ. Restrict query time ranges and log groups to control scanned data. A regex filter should not be assumed to use a field index or reduce scan charges, and a result limit does not guarantee that only that many events are scanned.

Finally, distinguish regex from wildcard matching and surrounding configuration syntax. In regex, a dot generally matches any character and an asterisk repeats the preceding element; a standalone asterisk does not mean any string as it often does in a shell glob. Escaping may also occur at multiple layers when a pattern is embedded in JSON, a programming-language string, or infrastructure configuration. Validate the final pattern in the target service rather than assuming that success in a different regex tester guarantees compatibility.

## Learn Regular Expressions in lessons

1 short lesson on Regular Expressions in 1 certification course.

- DOP-C02: Regular Expressions: dialect-aware log patterns (Premium), https://app.blowstack.com/lessons/regular-expressions-dialect-aware-log-patterns-dop-c02
