Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Regex How To:
- Starts With:
- How to:
- Place the metasymbol '^' in front of your regex expression.
- Example:
- Starts with "cat":
- ^cat
- would match
- i want to pet the cat
- i don't like the cat
- wouldn't match
- I think the cat is cool
- I like the cat!
- Stars with "asdf":
- ^asdf
- would match
- asdfghjkl;'
- wouldn't match
- qasdf
- Ends With:
- How to:
- Place the metasymbol '$' in the end of your regex expression.
- Example:
- Ends with "cat":
- cat$
- would match
- i want to pet the cat
- i don't like the cat
- dude you're such a copycat
- wouldn't match
- I think the cat is cool
- I like the cat!!
- Wildcards:
- . \d \w
- Anything (Matches anything)
- How to:
- Place the metasymbol '.' anywhere to match any possible character
- Example:
- Match 3-letter words ending in "at" (first character wildcard):
- .at
- would match anything containing
- cat
- bat
- 5at
- %at
- would not match
- at
- cab
- Digits (0 1 2 3 ... 9)
- How to:
- Place '\d' as a replacement for any digit.
- Example:
- Match a phone number of type (###)###-####
- (\d\d\d)\d\d\d-\d\d\d\d
- would match anything containing
- (123)456-7890
- (351)125-6382
- would not match
- (123) 456 7890
- Match a time ##:##
- \d\d:\d\d
- would match anything containing
- 12:34
- 01:59
- would not match
- 1:59
- 1234
- Word Letters (a b c ... z _ 0 1 2 ... 9 )
- How to:
- Place '\w' as a replacement for any word letter/digit.
- Example:
- Match any 5 letter word (can contain underscore or digits)
- \w\w\w\w\w
- would match anything containing
- asdfg
- as4fg
- 23456
- as4f_
- would not match
- as@fg
- as fg
- ~2345
- Other Metasymbols:
- \ | ( ) + *
- Backslash
- It's just a metasymbol for other things, like \d \w \s
- If you want to match just a "\", then write "\\", and it will match as "\"
- Pipe (or)
- How to:
- Place a pipe | between your options
- Example:
- cat|dog|pig|goat
- would match anything containing
- cat
- dog
- pig
- goat
- Groups (useful in conjunction with everything else, semi useless alone)
- How to:
- Place the group in ( )
- Example:
- (a|c)def
- would match anything containing
- adef
- cdef
- wouldn't match
- acdef
- def
- I like (cat|dog|pig|goat)s
- would match anything containing
- I like cats
- I like dogs
- I like pigs
- I like goats
- One or more repeating
- How to:
- Place a '+' after the group or character to repeat one or more times
- Example:
- Match an a, 1 or more b's, then c
- ab+c
- would match anything containing
- abc
- abbc
- abbbbbbbbbbbbbbbbbbbbbbbc
- wouldn't match
- ac
- abdc
- Match w, then the group xy any amount of times, then z
- w(xy)+z
- would match anything containing
- wxyz
- wxyxyxyxyxyxyxyxyz
- would not match
- wxyxz
- wz
- wxy
- xyz
- Zero or more repeating
- How to:
- Place a '*' after the group or character to repeat zero or more times
- Example:
- Match an a, 0 or any number of b's, then c
- ab*c
- would match anything containing
- ac
- abc
- abbbbbbbbbbbbbbbbbbbbbbbc
- wouldn't match
- abdc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement