REGEX

Recommended Readings

What is regular expression?

A regular expression is a sequence of characters that define a search pattern. Usually such patterns are used by string searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory. β€” Wikipedia It works similar to when we are searching something on Google but in a more advanced and specific way.

Why it matters?

    Remove human errors when it come to countless of data sorting or wrangling
    Save you time and effort - once you written it once, it's reuable

The basics

Character class

[]
​
​

Cookbook

1
//1st word including hyphen eg.Bethune-Cookman
2
^\w+\b[-]{0,1}(\w+)?
3
​
4
​
5
1st 2 words with/without "'s"
6
^\w+\s\w+[\']?s?
7
​
8
//2 words only
9
^\w+\s\w+$
10
​
11
// searching ???
12
.+\(.+\)
13
​
14
// Last word in parenthesis
15
\(([^)]*)\)[^(]*$
Copied!

HEX codes

1
[A-Fa-f0-9]{6} // single
2
​
3
​
4
// HEX codes from "Primary: 0050A3 Secondary: FFFFFF"
5
\s[a-zA-Z0-9]{6,}
Copied!

NCAA

1
// team names
2
(\w*Alt\w*){0,1}((-\s)?\w*ALT\w*){0,1}","")
3
​
4
// breaking
5
^\w+[-]?\w // catch hyphenated compound
6
^\w+\b[-]{0,1}[\w+]? // break two words
7
^\w+[']?\w?\s\w+[\']?s?" // break words with apostrophe
8
​
9
\w+\s\w+$ // capture last 2 words
10
\w+$ // capture last word
11
​
12
// optimized
13
^[\w's&-.]+[ &]?[\w's&.]+
14
​
15
​
16
// workflow
17
// 1. break school with State
18
^[\w's&-.]+[ &]?[\w's&.]+ [State]+
19
^[\w's&-.]+[ &]?[\w's&.]+ [StateUniversity]+
Copied!
​
Last modified 1yr ago