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
No Match

r"
"
gmi

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"\b(?<!\.)[!#$%&'*+\-/=?^_`{|}~\w][!#$%&'*+\-/=?^_`{|}~\.\w]{0,}[!#$%&'*+\-/=?^_`{|}~\w][@]\w+(?:[-.]\w+)*" test_str = ("Check this one too for my friend: a&d@somedomain.com\n" "This looks weired but it is valid email a*d@somedomain.com\n" "Same here really odd one - a/d@somedomain.com\n" "What can you say for this one huh ? tim@yahoo.abc.com\n" "Non-Matches:\n" "This is invalid _ .abc@somedomain.com\n" "Not at all valid abc.@somedomain.com\n" "angle brackates are not allowed too a>b@somedoma\n\n" "visa, last 4 digits 3208; my dob is 12/31\n" "this afternoon the 800 number. I live\n" "number (ending in 6436), however it has a cvv to capture.\n" "that a payment on 5183 is due 6/21. I am prepared to get my cvv\n" "was sent ending in 4859 does not work\n" "check it, my ID is 411978813 not present in my data.\n" "my number is _84 24 028458888 they gave a reference\n" "Vregistration number 280757578 and having another data is also available.\n" "confirmation code 100453052 and ave yet to get those details in to my account\n" "my number again : +55(11)995149340 thanks and regards\n" "has an item number 253778620 please can you project this details to client.\n" "it card ending in 7656 never receive the pin for my cc card details.\n" "my card pin not received but I arrive october 16 2018 thru UPS but until\n" "My old passwOrD was kufd87jhj_)kj\n" "Pin: 901608 7383 psdwd is p6%ss_09gt^4v}\n" "Mobile unlock key for user_1 is: 7305q-efd0t-0i2i7 and\n" "for user_2 is fgth8d-c7760-wwrb3 and the password for user_1 is gth8d_kj212\n" "The CEO's UUID is provided here - 7FC747F5-FB0D-4065-82B1-3F38D2988ECE and\n" "the COO's is also provided: 31EB3A28-896A-40C2-B0BE-0C21EA3F4FBB4\n" "My ipv4 address is 392.168.0.42; but 192.168.0.420 is not an address.\n" "The hacker's ip is 168.212.226.204 and he has no idea that I who's ip is not \n" "Here are some ipv6 hosts which I'll be using to confuse the hacker: fe80::1 or 2342:32fd::2d32\n" "fe80:33333::1ff:fe23:4567:890a this is invalid ip and my pswd: h_08$#fRdc54@\n" "ohn, please get that article on www.linkedin.com to me by 5:00PM or 7th of December 1976\n" "If you have any other plans on February 16th then let me know or on 15/02/2009 and \n" "My birthday is on 12th of oct and my wife's date of birth is december 39, 1991\n" "You can reach me at (519)-236-2723x341 or get in touch with my associate at harold.smith@gmail.com\n" "Here is your new TsyS_passwrd: 1LK2XP0S and the system pswd is nwfun&b_08G\n" "Here is your new tsYs-paSswOrd - NsoCxbo4 and the passwrd is pass_09Hg54v}\n" "my person no. is 67542090 and nbk is zk87ghY but my friend's nbk starts from nb which is nbh768i\n" "please go to this link and upload your documents - http://www.bofa.com/\n" "follow this link: https://www.ml.com/ and make a payment of $35.00\n" "click on this link and get hacked banofamerica.com/page1/hacked/\n" "this is official website www.bankofamerica.com\n" "your loan file number is pfm657890345120 and loan balance is $34,67.89\n" "loan reference number is provided as 657890236758409 and monthly installment is $ 12.50K\n" "you have received the loan reimbursement of 2.9 million dollars\n" "this month loan amount is 54,98.45K and transaction no is a72h6j98\n" "this client transaction num: A7287509j is invalid but unlock key is 57zod-8ftf9-p0fu3 is valid\n" "The party id - 12345678900 is actually a customer whose loan number is 12345678\n" "customer: 09876543211 and loan num 123456789\n" "The credit scores are: fico 123 and Port Number 64438\n" "credit score is 987 and unlock key: cmydh-gpgke-3xehb\n" "234 678 and unlock key - dzdb9950bsi3su4\n" "984 652 836 and access the drive \\\\Dfs.uk.ml.com\\london\\gtsshared\\ProjectDelivery\\TI\n" "License key: B40ZH2PLKRHUN7RD8P7PQ6MHSMFC5HPUG2YJW-142-101-064-0E\n" "License key: EDJWMHCS3SGAPPCCBFLARYZMX7ZMWRFLKM-142-100-934-AD\n" "this is some kind of key I guess 490853-104214-320254-230868-710347-170533-492679-288992\n" "Even this is some kind of key 090640-265034-253704-024937-145321-484748-151778-459074\n" "this mail is regarding employee termination of user nbkh7h0 \n" "ssh2: RSA SHA256:hKeAu/WKftYpqXHW4ReTrv3wXYbkdkdNgnEyGHkMCsSPU\n" "URGENT TERMINATION: User - zk8hy5f \n" "RSA key fingerprint is SHA256:N5JRToYZAAlaroppYu7R7aB06ihyqvCVrYfLxLC\n" "we have received a terminatION request for user zk4gh8y\n" "access key: hba1x oc6a4 fx7uq and drive link: \\\\nrch3000-nas03.bankofamerica.com\\nrch0300_user033$\\8tzkj6h\\homespace\n" "Security code: 690037 and Port: 6443\n" "unlock key tnaxx-9hbx7-pdg33 Port 6443\n" "\"Traceback (most recent call last):\n" " File \"\"/path/to/example.py\"\", line 4, in <module>\n" " greet('Chad')\n" " File \"\"/path/to/example.py\"\", line 2, in greet\n" " print('Hello, ' + someon)\n" "NameError: name 'someon' is not defined\"\n" "pbrun7.5.0-12[404]: Request rejected by pbmasterd on server1234.abcd.yyyy.com. \n" "Error: \"sh: pbrun: command not found\"\n" " http://ah-1004452-001.corp.bankofamerica.com:8080/\n" "production server lmumm01spdbd1.corp.bankofamerica.com:49125 is depricated\n" "tcp link is given as - tcp://ah-1005300-001.sdi.corp.bankofamerica.com:8343\n") matches = re.finditer(regex, test_str, re.MULTILINE | re.IGNORECASE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html