Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gm

Test String

Substitution

Processing...

Code Generator

Generated Code

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "(export )?const (.* \\= \\()(.*)(\\).*\\()(?'fileContent'(\\n.*)+)\\3\\.(?'varName'[^\\}]*)\\}(?'restOfPropLine'.*$)(?'restOfFileContent'(\\n.*)+>\\n\\);)"; final String string = "export const AffiliateAccountBIFields = (props) => (\n" + " <SimpleShowLayout {...props} variant=\"one\">\n" + " <UrlField source=\"bi_url\" options={{ target: '_blank' }} />\n" + " <ProtectedBIConfigField source=\"bi_settings\" />\n" + " </SimpleShowLayout>\n" + ");\n\n" + "export const AffiliateAccountGeneralFields = (props) => (\n" + " <SimpleShowLayout {...props}>\n" + " <TextField source=\"name\" />\n" + " <ReferenceField\n" + " linkType=\"show\"\n" + " label=\"Platform Provider\"\n" + " addLabel\n" + " source=\"platform_provider_id\"\n" + " reference=\"accounts/platform-providers\"\n" + " allowEmpty\n" + " >\n" + " <TextField addLabel source=\"name\" />\n" + " </ReferenceField>\n" + " <ReferenceField\n" + " label=\"Company\"\n" + " addLabel\n" + " source=\"company_id\"\n" + " reference=\"companies\"\n" + " linkType=\"show\"\n" + " >\n" + " <TextField addLabel source=\"name\" />\n" + " </ReferenceField>\n" + " <AccountManagerField\n" + " label=\"Account Manager\"\n" + " source=\"account_manager_id\"\n" + " addLabel\n" + " />\n" + " <PortfolioField label=\"Portfolio\" source=\"portfolio_id\" addLabel />\n" + " <FunctionField\n" + " label=\"Status\"\n" + " addLabel\n" + " source=\"activity_status\"\n" + " render={(record) => {\n" + " if (!record.activity_status) return null;\n" + " const humanizedStatus = get(\n" + " ACTIVITY_STATUS_OPTIONS,\n" + " `${record.activity_status}.name`\n" + " );\n" + " if (!humanizedStatus) {\n" + " // eslint-disable-next-line no-console\n" + " console.warn(`Cannot obtain humanized value for activity_status: ${record.activity_status}`);\n" + " }\n" + " return (\n" + " <TextField\n" + " source=\"activity_status\"\n" + " record={{\n" + " ...record,\n" + " activity_status: humanizedStatus || record.activity_status,\n" + " }}\n" + " />\n" + " );\n" + " }}\n" + " />\n" + " <UrlField source=\"affiliate_system_link\" options={{ target: '_blank' }} />\n" + " <CopyToClipboardField source=\"username\">\n" + " <TextField source=\"username\" />\n" + " </CopyToClipboardField>\n" + " <ProtectedField source=\"password\" />\n" + " <CopyToClipboardField source=\"email\">\n" + " <TextField source=\"email\" />\n" + " </CopyToClipboardField>\n" + " <TextField source=\"security_question\" />\n" + " <ProtectedField source=\"security_answer\" />\n" + " <TextField source=\"type\" />\n" + " <BooleanField source=\"tracking_enabled\" />\n" + " <TextField source=\"tracking_variable_name\" />\n" + " <TextField source=\"external_affiliate_id\" />\n" + " <TextField source=\"revenue_posting_code\" />\n" + " <BooleanField source=\"loss_carry_forward\" />\n" + " <DateField source=\"updated_at\" locales=\"en-GB\" showTime />\n" + " <DateField source=\"created_at\" locales=\"en-GB\" showTime />\n" + " </SimpleShowLayout>\n" + ");\n\n" + "export const AffiliateAccountTabs = (props) => {\n" + " if (!props.record) {\n" + " return null;\n" + " }\n\n" + " return (\n" + " <TabbedShowLayout>\n" + " <AffiliateAccountGeneralFields {...props} label=\"General\" />\n" + " <AffiliateAccountBrands {...props} label=\"Brands\" />\n" + " <AffiliateAccountDeals {...props} label=\"Deals\" />\n" + " {props.hasBIPermission && (\n" + " <AffiliateAccountBIFields {...props} label=\"BI Settings\" />\n" + " )}\n" + " </TabbedShowLayout>\n" + " );\n" + "};\n" + "AffiliateAccountTabs.propTypes = {\n" + " record: PropTypes.object,\n" + " hasBIPermission: PropTypes.bool.isRequired,\n" + "};\n\n" + "export const Show = (props) => (\n" + " <AutoLoginStatusDialog messages={messages}>\n" + " <AORShow\n" + " {...props}\n" + " title={(\n" + " <Title\n" + " resource=\"Affiliate Account\"\n" + " type=\"show\"\n" + " buttons={[\n" + " <FavoriteButton\n" + " record={{ _id: props.match.params.id }}\n" + " resource={props.resource}\n" + " style={{ height: '25px' }}\n" + " />,\n" + " ]}\n" + " />\n" + " )}\n" + " actions={(\n" + " <ActionsPanel\n" + " customButtons={[\n" + " <AutoLoginButton\n" + " record={{ _id: props.match.params.id }}\n" + " resource={props.resource}\n" + " resourceConfig={autoLoginConfig}\n" + " label=\"Auto Login\"\n" + " />,\n" + " ]}\n" + " />\n" + " )}\n" + " >\n" + " <AffiliateAccountTabs hasBIPermission={props.hasBIPermission} />\n" + " </AORShow>\n" + " <NotesView isShow match={props.match} resource={props.resource} />\n" + " </AutoLoginStatusDialog>\n" + ");\n" + "Show.propTypes = {\n" + " match: PropTypes.shape({\n" + " params: PropTypes.shape({\n" + " id: PropTypes.string,\n" + " }),\n" + " }),\n" + " resource: PropTypes.string,\n" + " hasBIPermission: PropTypes.bool.isRequired,\n" + "};\n\n" + "export default connect((state) => ({\n" + " hasBIPermission: checkPermission(state.auth.permissions, '*', 'bi_user'),\n" + "}))(Show);\n"; final String subst = "$1const $2{ ${varName}, ...$3 }$4${fileContent}${varName}}${restOfPropLine}${restOfFileContent}"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); // The substituted value will be contained in the result variable final String result = matcher.replaceAll(subst); System.out.println("Substitution result: " + result); } }

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html