# Regex parse in CloudWatch Logs Insights

Logs Insights queries · Updated 16 November 2026

**Short answer:** Logs Insights accepts regular expressions between slashes. Use them in filter (@message like /pattern/ or =~) to match events, and in parse with named capture groups, (?<name>...), to turn parts of a text line into fields you can filter, group and sort on.

## The query

Given application lines such as `2026-11-16 09:12:44 WARN [orders] payment declined user=u-123 ms=842`:

```
filter @message =~ /(WARN|ERROR)/
| parse @message /(?<level>WARN|ERROR) \[(?<component>[^\]]+)\] (?<text>.*?) user=(?<user>\S+) ms=(?<ms>\d+)/
| filter ispresent(component)
| stats count(*) as n, avg(ms) as avgMs by level, component
| sort n desc
```

## How it works

1. `filter @message =~ /(WARN|ERROR)/` keeps only lines matching the regex. `=~` and `like` behave the same with a regex; `like` with a quoted string is a plain substring match.
2. `parse @message /…/` applies the regex to each event. Every named group, `(?<name>…)`, becomes a field with that name. Here that's `level`, `component`, `text`, `user` and `ms`.
3. Brackets and other special characters must be escaped, as in `\[` and `\]`. `.*?` is a lazy match so `text` stops at `user=`.
4. `filter ispresent(component)` drops events where the regex didn't match; parse leaves the fields empty rather than removing the event.
5. `stats` then uses the extracted fields like any other. `avg(ms)` works because a value captured as digits can be used in arithmetic.

Test a new pattern on a small sample first: run it with `| display @message, level, component, user | limit 20` over a few minutes of logs and check that every column is filled before you aggregate.

## Regex patterns worth keeping

| Goal | Pattern |
|---|---|
| Case-insensitive match | `/(?i)timeout/` |
| Whole word only | `/\berror\b/` |
| UUID (Lambda request ID) | `/(?<rid>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/` |
| IPv4 address | `/(?<ip>\d{1,3}(\.\d{1,3}){3})/` |
| HTTP status in an access line | `/" (?<status>[1-5]\d\d) /` |
| AWS region code | `/(?<region>[a-z]{2}(-[a-z]+)+-\d)/` |
| Exception class | `/(?<exc>[\w.]+(Exception\|Error))/` |

## Error patterns for Lambda and API Gateway

**Lambda runtime errors**, grouped by type:

```
filter @message like /Runtime\.\w+|errorType/
| parse @message /(?<errType>Runtime\.\w+)/
| stats count(*) as n by errType
| sort n desc
```

**API Gateway execution logs**, which put the request ID in brackets at the start of each line:

```
filter @message like /Method completed with status: 5\d\d/
| parse @message /^\((?<reqId>[^)]+)\) Method completed with status: (?<status>\d+)/
| stats count(*) as n by status
```

## Glob or regex, and what regex can't do here

- For simple, fixed layouts a glob is easier to read: `parse @message "user=* ms=*" as user, ms`. Switch to regex when the layout varies or you need validation.
- There's no regex-based replace function; `replace()` substitutes a literal string. Extract the part you want with a capture group instead.
- Parsing costs nothing extra beyond the data scanned, but complex patterns over huge ranges are slower; filter first with a cheap substring match, then parse.
- Only *named* groups become fields. Plain parentheses still work for alternation and repetition, as in `(\.\d{1,3}){3}`, but produce no column.
- One `parse` command takes one pattern. To extract from differently shaped lines, use several `parse` commands, each with its own field names, and combine them with `coalesce()`.

## Questions

### Does CloudWatch Logs Insights support regex?

Yes. Put the pattern between slashes in filter (like or =~) or in parse, where named capture groups become fields.

### How do I make a Logs Insights regex case-insensitive?

Start the pattern with (?i), for example filter @message like /(?i)error/.

### Can I replace text with a regex in Logs Insights?

Not directly; replace() works with literal strings. Use parse with a named capture group to extract the part you need.

## When this happens away from your desk

Getting a regex right is slow on a small screen, so it pays to save working queries. CloudConsole, a phone app for AWS, is built to show alarms and the related log lines on your phone.

[About the CloudConsole app](https://cloudconsole.dev/aws-console-app) · [Download on the App Store](https://apps.apple.com/app/cloudconsole-for-aws/id6835419710)

## Sources

- [CloudWatch Logs Insights query syntax](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html)
- [Analyzing log data with CloudWatch Logs Insights](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html)

## Related

- [Parse JSON fields in CloudWatch Logs Insights](https://cloudconsole.dev/logs-insights/parse-json-fields)
- [CloudWatch Logs Insights syntax cheat sheet](https://cloudconsole.dev/logs-insights/syntax-cheat-sheet)
- [API Gateway 5xx errors with Logs Insights](https://cloudconsole.dev/logs-insights/api-gateway-5xx-errors)
- [Find Lambda timeouts with CloudWatch Logs Insights](https://cloudconsole.dev/logs-insights/lambda-timeouts)
- [CloudWatch Logs Insights query examples](https://cloudconsole.dev/logs-insights/query-examples)
- [bin() in CloudWatch Logs Insights](https://cloudconsole.dev/logs-insights/bin-time-series)

---

Source: https://cloudconsole.dev/logs-insights/regex-parse

CloudConsole is an independent iPhone and Android app for Amazon Web Services by Cloudconcur Ltd. It is not affiliated with Amazon Web Services.
