We’re improving Formula Genius. We’re redesigning the app and fixing issues along the way. Sorry for any disruption, and thank you for your patience.
Regex advanced

How to Use Named Capture Groups in Regex?

Named capture groups simplify extracting specific data from strings, making it easier to retrieve first names, last names, and domains.

Formula Genius can generate a draft from a plain-English prompt. Supported formulas can be checked with sample cases; review the coverage on the result.

The formula

Prompt

"Use named capture groups to extract first name, last name, and domain from a formatted string"

Regex
(?P<first_name>\w+)\s(?P<last_name>\w+)@(?P<domain>\w+\.\w+)

This regex pattern captures first names, last names, and domains from a formatted email-like string.

Step-by-step breakdown

  1. The pattern starts with '(?P<first_name>\w+)' to capture the first name.
  2. Next, '\s' matches the space between the first and last names.
  3. Then, '(?P<last_name>\w+)' captures the last name.
  4. The '@' symbol indicates the start of the domain part.
  5. Finally, '(?P<domain>\w+\.\w+)' captures the domain, including the top-level domain.

Edge cases & warnings

  • Input with missing last name will not match the pattern.
  • Strings with additional spaces may cause unexpected results.
  • Invalid domain formats (e.g., missing TLD) will not be captured.
  • Names with special characters (e.g., hyphens) may not be captured correctly.
  • Input strings that do not follow the expected format will fail to match.

Examples

Prompt

"John Doe@example.com"

Regex
first_name: John, last_name: Doe, domain: example.com
Prompt

"Jane Smith@domain.org"

Regex
first_name: Jane, last_name: Smith, domain: domain.org

Frequently asked questions

What are named capture groups?

Named capture groups allow you to assign names to specific parts of your regex pattern for easier reference.

Can I use this regex for different formats?

This regex is tailored for a specific format; variations may require adjustments.

How do I test my regex pattern?

You can test your regex pattern using online regex testers or built-in tools in programming languages.

Can't find what you need?

Describe a formula in plain English. Formula Genius drafts and explains it, and can check supported formulas on sample cases.