https://github.com/davidhu2000/regex_crash_course
A crash course on how to use Regular Expressions
https://github.com/davidhu2000/regex_crash_course
pattern-matching regex regex-pattern regexp
Last synced: 6 months ago
JSON representation
A crash course on how to use Regular Expressions
- Host: GitHub
- URL: https://github.com/davidhu2000/regex_crash_course
- Owner: davidhu2000
- Created: 2017-04-20T16:31:36.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-21T04:38:04.000Z (about 9 years ago)
- Last Synced: 2025-03-25T01:44:28.506Z (over 1 year ago)
- Topics: pattern-matching, regex, regex-pattern, regexp
- Size: 1000 Bytes
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# A crash course on how to use regular expressions
**NB:** this crash course uses Ruby as the default language.
Regular expressions are used to match patterns in strings. This repo serves as a crash course on how to use regular expression.
## Creating a regex object
There are three ways to create a `regex` object.
/.../
%r{...}
Regexp.new(...)
where `...` is the desired pattern.
## Methods
'hello' =~ /world/ #=> nil
'banana' =~ /ana/ #=> 1
`=~` is Ruby's basic pattern matching operator, where one side is a string, and the other side is regular expression. This method returns the index of the first match in the string.
/adam/.match('madam') #=>
/yolo/.match('careful') #=> nil
`#match` returns a MatchData object.
There are other methods that can use regular expression to pattern match. For example, `String#split` and `Array#scan`.
## Characters Classes
| character | function |
|:---------:|------------------------------------------|
| `.` | any single character except new line |
| `[abc]` | any character in the set |
| `[^abc]` | any character not in the set |
| `[a-z]` | any character from `a` to `z`. |
| `\w` | any word character `[a-zA-Z0-9_]` |
| `\W` | any non-word character `[^a-zA-Z0-9_]` |
| `\d` | any digit character `[0-9]` |
| `\D` | any non-word character `[^0-9]` |
| `\s` | a whitespace character |
| `\S` | any non-word character |
| `\h` | a hexdigit character `[0-9a-fA-F]` |
| `\H` | a non-hexdigit character `[^0-9a-fA-F]` |
### Escaped Special Characters
These characters have special meanings in regular expressions and needs to be escaped if you want to match that exact character.
`\.`, `\\`, `\*`, `\+`, `\?`