Regular Expressions
- What is a regular expression
- A regular expression is also called a pattern or regex
- Sequence of characters
- Forms a search pattern
- Normally written between forward slashes
/pattern/ - Used to check if a string contains the pattern and also to extract portions that match
- Characters
- A regex has 2 types of characters
- regular character - with literal meaning
- meta character - special meaning
- Use Cases & Examples
seriali[sz]e- To locate the same word with different spellings- To locate email addresses in the text
- Syntax Highlighting
- Search engines
- Quantification/Repetition
- Quantifier - is a construct that specifies how often the preceeding element is allowed to occur.
a?- zero or one occurence of aa*- zero or more of aa+- one or more of a- `a{3} - exactly 3 occurrences of a
- `a{3,} - 3 or more occurences of a
- `a{3,6} - between 3 and 6 occurences of a
- Grouping
- Grouping constructs specify a group
- [abc] - a single character of a,b or c
- [^abc] - a single character except a, b or c
- [a-zA-Z] - a single character in the range of a-z or A-Z
- (..) - everything enclosed
- Metacharacters
^- Start of line$- end of line\A- start of string\z- end of string.- any single character\s- any whitespace character\S- any non whitespace character\d- any digit\D- any non digit\w- any word character\W- any non word character
- Logical
-
(a b) - a or b
-
- Ruby Examples
- Ruby has built in support for regex
=~operator is used to match a regex and a string. Order does not matter- If match is found, the operator returns index of first match in string.
- Returns nil otherwise
"apple" =~ /ap/ #=> 0"apple" =~ /fruit/ #=> nilmatchmethod returns a match data object"apple".match(/ap/) #<MatchData "ap">
- Resources
-
Categories
-
Database
-
Programming
-
Workflow
-
Devops
-
Architecture
-
Ui
-
Frameworks
-
Blogging