• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips

Regular Expression

Regular Expressions Cheat sheet

June 13, 2014 By Ravi Shankar Leave a Comment

Regular Expressions form sequence of characters which can be used for pattern matching. They are quite powerful tool for finding or matching combination of characters.

Special Characters in Regular Expression

  1. backslash ( \ ) – Used for matching special character by proceeding with backslash Eg:- For matching period in a string use backslash before period (\.) as period is a special character.
  2. asterisk ( * ) – Refers to zero or more occurrences of regular expression.
  3. plus ( + ) – Refers to one or more occurrences of regular expression.
  4. question mark ( ? ) – Used for special optional characters.
  5. period ( . ) – Matches any character except newline.
  6. caret ( ^ ) – Refers to start of the current line.
  7. ampersand ( & ) – Ampersand used for matching both expression
  8. Or sign ( | ) – Used for matching either expression.
  9. dollar sign ( $ ) – Refers to end of entire regular expression.
  10. square brackets ( [ ] ) – Matches any one character in the square brackets. If ^ sign is present before the contents in square brackets then characters except those mentioned inside square brackets are matched.

Regular Expression example

/^[a-z0-9]{6,10}$/

The above expression specifies the start of the string as either alphabets or numbers with characters count minimum as 6 and maximum as 10. This would match string such as david007 and not david-007 as hyphen is not included in the RegEx pattern. And to fix this change the pattern to include hyphen. /^[a-z0-9-]{6,10}$/

Filed Under: Programming Tagged With: RegEx, Regular Expression

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup
  • Show hidden files and folders on Mac
  • How to request a refund for apps in App Store

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy