Advertisement
teleias

How To Regex (Short Guide)

Jul 6th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. Regex How To:
  2.  
  3. Starts With:
  4. How to:
  5. Place the metasymbol '^' in front of your regex expression.
  6. Example:
  7. Starts with "cat":
  8. ^cat
  9. would match
  10. i want to pet the cat
  11. i don't like the cat
  12. wouldn't match
  13. I think the cat is cool
  14. I like the cat!
  15. Stars with "asdf":
  16. ^asdf
  17. would match
  18. asdfghjkl;'
  19. wouldn't match
  20. qasdf
  21.  
  22. Ends With:
  23. How to:
  24. Place the metasymbol '$' in the end of your regex expression.
  25. Example:
  26. Ends with "cat":
  27. cat$
  28. would match
  29. i want to pet the cat
  30. i don't like the cat
  31. dude you're such a copycat
  32. wouldn't match
  33. I think the cat is cool
  34. I like the cat!!
  35.  
  36. Wildcards:
  37. . \d \w
  38. Anything (Matches anything)
  39. How to:
  40. Place the metasymbol '.' anywhere to match any possible character
  41. Example:
  42. Match 3-letter words ending in "at" (first character wildcard):
  43. .at
  44. would match anything containing
  45. cat
  46. bat
  47. 5at
  48. %at
  49. would not match
  50. at
  51. cab
  52.  
  53. Digits (0 1 2 3 ... 9)
  54. How to:
  55. Place '\d' as a replacement for any digit.
  56. Example:
  57. Match a phone number of type (###)###-####
  58. (\d\d\d)\d\d\d-\d\d\d\d
  59. would match anything containing
  60. (123)456-7890
  61. (351)125-6382
  62. would not match
  63. (123) 456 7890
  64. Match a time ##:##
  65. \d\d:\d\d
  66. would match anything containing
  67. 12:34
  68. 01:59
  69. would not match
  70. 1:59
  71. 1234
  72.  
  73. Word Letters (a b c ... z _ 0 1 2 ... 9 )
  74. How to:
  75. Place '\w' as a replacement for any word letter/digit.
  76. Example:
  77. Match any 5 letter word (can contain underscore or digits)
  78. \w\w\w\w\w
  79. would match anything containing
  80. asdfg
  81. as4fg
  82. 23456
  83. as4f_
  84. would not match
  85. as@fg
  86. as fg
  87. ~2345
  88.  
  89. Other Metasymbols:
  90. \ | ( ) + *
  91. Backslash
  92. It's just a metasymbol for other things, like \d \w \s
  93. If you want to match just a "\", then write "\\", and it will match as "\"
  94. Pipe (or)
  95. How to:
  96. Place a pipe | between your options
  97. Example:
  98. cat|dog|pig|goat
  99. would match anything containing
  100. cat
  101. dog
  102. pig
  103. goat
  104. Groups (useful in conjunction with everything else, semi useless alone)
  105. How to:
  106. Place the group in ( )
  107. Example:
  108. (a|c)def
  109. would match anything containing
  110. adef
  111. cdef
  112. wouldn't match
  113. acdef
  114. def
  115. I like (cat|dog|pig|goat)s
  116. would match anything containing
  117. I like cats
  118. I like dogs
  119. I like pigs
  120. I like goats
  121.  
  122. One or more repeating
  123. How to:
  124. Place a '+' after the group or character to repeat one or more times
  125. Example:
  126. Match an a, 1 or more b's, then c
  127. ab+c
  128. would match anything containing
  129. abc
  130. abbc
  131. abbbbbbbbbbbbbbbbbbbbbbbc
  132. wouldn't match
  133. ac
  134. abdc
  135. Match w, then the group xy any amount of times, then z
  136. w(xy)+z
  137. would match anything containing
  138. wxyz
  139. wxyxyxyxyxyxyxyxyz
  140. would not match
  141. wxyxz
  142. wz
  143. wxy
  144. xyz
  145. Zero or more repeating
  146. How to:
  147. Place a '*' after the group or character to repeat zero or more times
  148. Example:
  149. Match an a, 0 or any number of b's, then c
  150. ab*c
  151. would match anything containing
  152. ac
  153. abc
  154. abbbbbbbbbbbbbbbbbbbbbbbc
  155. wouldn't match
  156. abdc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement