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

package main import ( "regexp" "fmt" ) func main() { var re = regexp.MustCompile(`(?m)(?s)^(.*?)\*\*\*\*\*\*\*`) var str = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus accumsan risus id ex dapibus sodales. Curabitur dui lacus, tincidunt vel ligula quis, volutpat mattis eros. 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. *********** Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean egestas urna facilisis massa posuere, quis accumsan erat ornare. Curabitur at dapibus nibh. Nam nec vestibulum ligula. Phasellus bibendum mi urna, ac hendrerit libero interdum non. Suspendisse semper non elit aliquam auctor. Morbi vel sem tortor. Donec a sapien quis erat condimentum consequat in ut sem. Quisque in tellus sed est lobortis ultricies sed vitae enim. I want to return this value in B1: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus accumsan risus id ex dapibus sodales. Curabitur dui lacus, tincidunt vel ligula quis, volutpat mattis eros. 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. 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. regexgoogle-spreadsheet shareeditcloseflag asked 3 hours ago aylim14 83 Try using stackoverflow.com/a/159140/5267751. – user202729 3 hours ago 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 add a comment 2 Answers active oldest votes up vote 0 down vote This simple RegEx should do the trick (although I don't know anything about Google-spreadsheets): [^*]*(?=\*{11}) The first match should be what you want. It simply matches anything up the the first '*', making sure it's followed by 11 stars. shareeditflag edited 3 hours ago answered 3 hours ago Poul Bak 1,680519 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 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 add a comment up vote 0 down vote 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. So, to match up to the first ******* substring, you may use (?s)^(.*?)\*\*\*\*\*\*\* or (?s)^(.*?)\*{7}. (?s) - a DOTALL modifier ^ - start of string (.*?) - Group 1: any 0+ chars as few as possible \*\*\*\*\*\*\* - 7 literal asterisk symbols. 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. If you just want to match any chars up to the first * in the string, your regex will simplify greatly to ^([^*]+) It matches ^ - start of string ([^*]+) - Capturing group 1: one or more chars other than *. shareeditdeleteflag answered just now Wiktor Stribiżew 284k16113183 add a comment Not the answer you're looking for? Browse other questions tagged regex google-spreadsheet or ask your own question. asked today viewed 15 times active today BLOG Developer Salaries in 2018: Updating the Stack Overflow Salary Calculator HOT META POSTS 33 Generic “Don't Do It” Answer 30 What effect do voting rings have on average post quality? 58 C tag usage, radical changes to tag wiki. Which policy to keep? ` for i, match := range re.FindAllString(str, -1) { fmt.Println(match, "found at index", i) } }

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 Golang, please visit: https://golang.org/pkg/regexp/