Regex for Between two strings
Extract content between two known markers.
The Between two strings regex is BEGIN(.*?)END — copy it below, or open it in the explainer for a token-by-token breakdown.
The pattern
BEGIN(.*?)END
Token-by-token breakdown
Every part of the pattern, left to right:
| Token | Meaning |
|---|---|
BEGIN | literal text “BEGIN” |
( | start of a capturing group |
.*? | zero or more (lazy — as few as possible) of any character |
) | end of group |
END | literal text “END” |
About this pattern
Number patterns appear constantly in form validation, log parsing, and data extraction. Regex is great for matching the shape but limited for semantic checks (range validation, mathematical constraints).
Quick usage in different languages
This exact pattern — with the correct escaping and idioms for each language:
- JavaScript:
/BEGIN(.*?)END/.test(value) - Python:
re.match(r"BEGIN(.*?)END", value) - Java:
Pattern.compile("BEGIN(.*?)END").matcher(value).matches() - C# / .NET:
Regex.IsMatch(value, @"BEGIN(.*?)END") - Go:
regexp.MustCompile(`BEGIN(.*?)END`).MatchString(value) - Ruby:
/BEGIN(.*?)END/.match?(value) - PHP:
preg_match('~BEGIN(.*?)END~', $value)
The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.
Common pitfalls
- Not anchored. Without ^ and $ this can match a substring anywhere in the input — add anchors if you need the whole value to conform.
- Greedy “.*”. A greedy .* / .+ can match more than intended. Use a lazy version (.*?) or a negated class ([^…]) to stop at the right place.
- Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the between two strings against a source of truth (database, API, or checksum) where it matters.
Related patterns
More patterns in the Numbers & text category:
- Quoted string
- Emoji (Unicode)
- Whitespace (multiple/leading/trailing)
- Positive decimal
- Alphanumeric
- Percentage (0-100%)
See also
Browse all 300 patterns in the library, or open this regex in the interactive explainer for a token-by-token breakdown, live testing, and code in seven languages.
Want more patterns? Browse the full library →