Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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

`
`
gm

Test String

Code Generator

Generated Code

#include <StringConstants.au3> ; to declare the Constants of StringRegExp #include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate Local $sRegex = "(?m)(?s)^(.*?)\*\*\*\*\*\*\*" Local $sString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus accumsan risus id ex dapibus sodales. " & @CRLF & _ "" & @CRLF & _ "Curabitur dui lacus, tincidunt vel ligula quis, volutpat mattis eros. " & @CRLF & _ "" & @CRLF & _ "In quis metus at ex auctor lobortis. Aliquam sed nisi purus. Sed cursus odio erat, ut tristique sapien interdum interdum. Morbi vel sollicitudin ante, non pellentesque libero. " & @CRLF & _ "" & @CRLF & _ "***********" & @CRLF & _ "" & @CRLF & _ "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean egestas urna facilisis massa posuere, quis accumsan erat ornare. " & @CRLF & _ "" & @CRLF & _ "Curabitur at dapibus nibh. Nam nec vestibulum ligula. Phasellus bibendum mi urna, ac hendrerit libero interdum non. Suspendisse semper non elit aliquam auctor. " & @CRLF & _ "" & @CRLF & _ "Morbi vel sem tortor. Donec a sapien quis erat condimentum consequat in ut sem. Quisque in tellus sed est lobortis ultricies sed vitae enim." & @CRLF & _ "I want to return this value in B1:" & @CRLF & _ "" & @CRLF & _ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus accumsan risus id ex dapibus sodales. " & @CRLF & _ "" & @CRLF & _ "Curabitur dui lacus, tincidunt vel ligula quis, volutpat mattis eros. " & @CRLF & _ "" & @CRLF & _ "In quis metus at ex auctor lobortis. Aliquam sed nisi purus. Sed cursus odio erat, ut tristique sapien interdum interdum. Morbi vel sollicitudin ante, non pellentesque libero. " & @CRLF & _ "Which is basically anything before the pattern *******. In Python, I can add the re.DOTALL to the .* but I can't get this to work in Google Sheets." & @CRLF & _ "" & @CRLF & _ "regexgoogle-spreadsheet" & @CRLF & _ "shareeditcloseflag" & @CRLF & _ "asked 3 hours ago" & @CRLF & _ "" & @CRLF & _ "aylim14" & @CRLF & _ "83" & @CRLF & _ "Try using stackoverflow.com/a/159140/5267751. – user202729 3 hours ago" & @CRLF & _ "Thanks! I did. But it didn't work. That was one of the first thing I tried. It kept returning everything and didn't stop at the *. The suggestion gave by Poul looks like it worked. But I can't say for sure until new data gets added to the spreadsheet. – aylim14 1 hour ago " & @CRLF & _ "add a comment" & @CRLF & _ "2 Answers" & @CRLF & _ "active oldest votes" & @CRLF & _ "up vote" & @CRLF & _ "0" & @CRLF & _ "down vote" & @CRLF & _ "This simple RegEx should do the trick (although I don't know anything about Google-spreadsheets):" & @CRLF & _ "" & @CRLF & _ "[^*]*(?=\*{11})" & @CRLF & _ "The first match should be what you want. It simply matches anything up the the first '*', making sure it's followed by 11 stars." & @CRLF & _ "" & @CRLF & _ "shareeditflag" & @CRLF & _ "edited 3 hours ago" & @CRLF & _ "answered 3 hours ago" & @CRLF & _ "" & @CRLF & _ "Poul Bak" & @CRLF & _ "1,680519" & @CRLF & _ "The *{11} actually doesn't matter. Sometimes, the separator is *****. Sometimes it's *\n*\n*\n* So I really just need it to stop at the first *. – aylim14 1 hour ago " & @CRLF & _ "I just substituted the [^*]* and I think it worked! Thanks so much. The spreadsheet gets updated once or twice a day.I'll monitor this to see if it works on new data. – aylim14 1 hour ago" & @CRLF & _ "add a comment" & @CRLF & _ "up vote" & @CRLF & _ "0" & @CRLF & _ "down vote" & @CRLF & _ "To make a dot match line breaks, you need to add (?s) to the pattern. To match any char, you may use a .. To match up to the leftmost occurrence, use lazy quantifier, *?. To actually extract a substring you need, wrap the part of the pattern you are interested in getting with capturing parentheses." & @CRLF & _ "" & @CRLF & _ "So, to match up to the first ******* substring, you may use" & @CRLF & _ "" & @CRLF & _ "(?s)^(.*?)\*\*\*\*\*\*\*" & @CRLF & _ "or (?s)^(.*?)\*{7}." & @CRLF & _ "" & @CRLF & _ "(?s) - a DOTALL modifier" & @CRLF & _ "^ - start of string" & @CRLF & _ "(.*?) - Group 1: any 0+ chars as few as possible" & @CRLF & _ "\*\*\*\*\*\*\* - 7 literal asterisk symbols." & @CRLF & _ "Note you cannot rely on a negated character class (that matches line breaks) if your substring may contain * chars, that is, ^([^*]*)\*\*\*\*\*\*\* won't work in those cases." & @CRLF & _ "" & @CRLF & _ "If you just want to match any chars up to the first * in the string, your regex will simplify greatly to" & @CRLF & _ "" & @CRLF & _ "^([^*]+)" & @CRLF & _ "It matches" & @CRLF & _ "" & @CRLF & _ "^ - start of string" & @CRLF & _ "([^*]+) - Capturing group 1: one or more chars other than *." & @CRLF & _ "shareeditdeleteflag" & @CRLF & _ "answered just now" & @CRLF & _ "" & @CRLF & _ "Wiktor Stribiżew" & @CRLF & _ "284k16113183" & @CRLF & _ "add a comment" & @CRLF & _ "Not the answer you're looking for? Browse other questions tagged regex google-spreadsheet or ask your own question." & @CRLF & _ "asked" & @CRLF & _ "" & @CRLF & _ "today" & @CRLF & _ "" & @CRLF & _ "viewed" & @CRLF & _ "" & @CRLF & _ "15 times" & @CRLF & _ "" & @CRLF & _ "active" & @CRLF & _ "" & @CRLF & _ "today" & @CRLF & _ "" & @CRLF & _ "BLOG" & @CRLF & _ "Developer Salaries in 2018: Updating the Stack Overflow Salary Calculator" & @CRLF & _ "HOT META POSTS" & @CRLF & _ "33 Generic “Don't Do It” Answer" & @CRLF & _ "30 What effect do voting rings have on average post quality?" & @CRLF & _ "58 C tag usage, radical changes to tag wiki. Which policy to keep?" & @CRLF & _ "" Local $aArray = StringRegExp($sString, $sRegex, $STR_REGEXPARRAYGLOBALFULLMATCH) Local $aFullArray[0] For $i = 0 To UBound($aArray) -1 _ArrayConcatenate($aFullArray, $aArray[$i]) Next $aArray = $aFullArray ; Present the entire match result _ArrayDisplay($aArray, "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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm