# Regex flags across languages

 

Use when writing, porting or debugging a regular expression and its flags/modifiers across languages and tools - JavaScript (g i m s u v y d), PCRE/PHP (i m s x u A D U J), Python re (re.I, re.M, re.S, re.X, re.A), .NET (RegexOptions), POSIX grep/sed. Also use for Laravel \`regex:\` validation rules and for catastrophic backtracking risks.

 

**Licence** MIT

 

**Source** delaparra-original

 [Download zip](<https://delaparra-services.nl/skills/regex-flags-cross-language/download>)   

# Regex flags across languages

 

The same regex can behave differently depending on the engine and its flags. Identify the target engine first, then write the pattern and flags for that engine, and test it with matching and non-matching examples.

 

## Workflow

 

1. **Name the engine:** JavaScript, PHP (PCRE2), Python ` re `, .NET, Java, or POSIX (` grep `, ` sed `). Say which one you are writing for. 
1. **Write test cases first:** at least 3 strings that must match and 3 that must not (including empty string and edge cases). 
1. **Write the pattern and flags,** anchored where possible (` ^…$ ` or ` \A…\z `). 
1. **Test** in the target language (a one-line script or unit test), not only in an online tester set to another flavour. 
1. **Check for backtracking risk:** nested quantifiers like ` (a+)+ `, ` (.*)* ` or ` (\w+\s?)*$ ` can take exponential time on long input (ReDoS). Prefer specific character classes and possessive/atomic groups where supported.

 

## Flag reference

    Meaning JavaScript PHP / PCRE Python .NET POSIX grep     Case-insensitive ` i ` ` i ` ` re.I ` ` IgnoreCase ` ` grep -i `   ` ^ `/` $ ` match at line breaks ` m ` ` m ` ` re.M ` ` Multiline ` default per line   ` . ` matches newline (dotall / single-line) ` s ` ` s ` ` re.S ` ` Singleline ` n/a   Ignore whitespace, allow comments (extended / verbose) n/a ` x ` ` re.X ` ` IgnorePatternWhitespace ` n/a   Unicode-aware ` u ` (or ` v ` for Unicode sets) ` u ` default for str default locale   ASCII-only classes n/a n/a ` re.A ` ` ECMAScript ` n/a   All matches (global) ` g ` use ` preg_match_all ` use ` re.findall ` / ` finditer ` ` Matches() ` default   Sticky (match at lastIndex only) ` y ` ` \G ` n/a ` \G ` n/a   Match indices ` d ` (hasIndices) ` PREG_OFFSET_CAPTURE ` ` m.span() ` ` Match.Index ` ` grep -b `   Anchored at start n/a ` A ` ` re.match ` ` \G ` n/a   ` $ ` only at very end n/a ` D ` (dollar-end-only) use ` \Z ` use ` \z ` n/a   Ungreedy by default n/a ` U ` (swaps ` * ` and ` *? `) n/a n/a n/a   Duplicate named groups n/a ` J ` n/a allowed n/a   Explicit capture only n/a ` n ` (PCRE2 10.30+, check version) n/a ` ExplicitCapture ` n/a   Right-to-left n/a n/a n/a ` RightToLeft ` n/a    

Notes: PHP's old ` e ` modifier (eval) is removed; use ` preg_replace_callback `. Perl's ` o ` (compile once) and ` l ` (locale) modifiers rarely matter in modern code.

 

## Laravel validation

 

```
'postcode' => ['required', 'regex:/^[1-9][0-9]{3}\s?[A-Z]{2}$/i'],
```

 

Use array syntax when the pattern contains ` | `, otherwise Laravel splits the rule string. Include delimiters and flags inside the string.

 

## Rules

 

- Don't use regex to parse HTML, JSON or full email/URL syntax; use a parser or the framework's validator (` email `, ` url `). 
- Always return the test cases along with the final pattern.

 

## Files

 

- ` LICENSE ` 
- ` SKILL.md ` 
- ` SOURCE.md `

 

## Related terms

 

- [/A (Anchored)](<https://delaparra-services.nl/knowledge-hub/a-anchored>) 
- [/a (ASCII-only)](<https://delaparra-services.nl/knowledge-hub/a-ascii-only>) 
- [/D (Dollar End Only)](<https://delaparra-services.nl/knowledge-hub/d-dollar-end-only>) 
- [/d (hasIndices)](<https://delaparra-services.nl/knowledge-hub/d-hasindices>) 
- [/e (Eval (deprecated))](<https://delaparra-services.nl/knowledge-hub/e-eval-deprecated>) 
- [/g (Global)](<https://delaparra-services.nl/knowledge-hub/g-global>) 
- [/I (Case-Insensitive (POSIX / grep))](<https://delaparra-services.nl/knowledge-hub/i-case-insensitive-posix-grep>) 
- [/i (Case-Insensitive)](<https://delaparra-services.nl/knowledge-hub/i-case-insensitive>) 
- [/J (Duplicate Named Groups)](<https://delaparra-services.nl/knowledge-hub/j-duplicate-named-groups>) 
- [/L (Locale-dependent)](<https://delaparra-services.nl/knowledge-hub/l-locale-dependent>) 
- [/m (Multiline)](<https://delaparra-services.nl/knowledge-hub/m-multiline>) 
- [/n (Explicit Capture)](<https://delaparra-services.nl/knowledge-hub/n-explicit-capture>) 
- [/o (Once (interpolate once))](<https://delaparra-services.nl/knowledge-hub/o-once-interpolate-once>) 
- [/r (Right-to-Left)](<https://delaparra-services.nl/knowledge-hub/r-right-to-left>) 
- [/s (DotAll (single line))](<https://delaparra-services.nl/knowledge-hub/s-dotall-single-line>) 
- [/U (Ungreedy)](<https://delaparra-services.nl/knowledge-hub/u-ungreedy>) 
- [/u (Unicode)](<https://delaparra-services.nl/knowledge-hub/u-unicode>) 
- [/v (Unicode Sets)](<https://delaparra-services.nl/knowledge-hub/v-unicode-sets>) 
- [/x (Extended / Verbose)](<https://delaparra-services.nl/knowledge-hub/x-extended-verbose>) 
- [/y (Sticky)](<https://delaparra-services.nl/knowledge-hub/y-sticky>)

 

[Installing an agent skill in Claude Code, Codex, Cursor and Copilot](<https://delaparra-services.nl/manuals/een-skill-installeren-in-je-ai-assistent>)

[HTML version](https://delaparra-services.nl/skills/regex-flags-cross-language)
