From 997a95cff22ff09d2615324dde42fb68a17289b6 Mon Sep 17 00:00:00 2001 From: Lars Windolf Date: Mon, 7 Aug 2023 23:22:28 +0200 Subject: [PATCH] Update Ruby Regex.md --- examples/Regex Examples/Ruby Regex.md | 116 ++++++++++++-------------- 1 file changed, 54 insertions(+), 62 deletions(-) diff --git a/examples/Regex Examples/Ruby Regex.md b/examples/Regex Examples/Ruby Regex.md index 75315c8aa..10d429fe3 100644 --- a/examples/Regex Examples/Ruby Regex.md +++ b/examples/Regex Examples/Ruby Regex.md @@ -1,94 +1,86 @@ Some simple examples for using regular expressions in Ruby scripts. -## Regular Expressions - -### 1\. Syntax +## 1. Syntax Compared to other scripting languages Ruby more behaves like [Perl](Perl) allowing to use regex seemlessly without the need for explicit objects in Python or a function in PHP. So you can just match something with putting a regular expression between two slashes: -
/search string/
+ /search string/ -### 2\. Simple String Checks +## 2. Simple String Checks Here are some syntax examples that check strings for certain content: -#### Basic Matching - -
text =~ /Ruby/       # Match for an unbound literal
-text =~ /^Ruby/      # Match literal at start of string
-text =~ /Ruby$/      # Match literal at end of string
-text =~ /^Ruby$/     # Match for exact string content
-text =~ /^$/         # Match empty string
-
+### Basic Matching + + text =~ /Ruby/ # Match for an unbound literal + text =~ /^Ruby/ # Match literal at start of string + text =~ /Ruby$/ # Match literal at end of string + text =~ /^Ruby$/ # Match for exact string content + text =~ /^$/ # Match empty string -#### Using different regex delimiters +### Using different regex delimiters -
%r/Ruby/               # / as commonly used delimiter
-%r@Ruby@               # @ as delimiter
-%r!Ruby!               # ! as delimiter
-
+ %r/Ruby/ # / as commonly used delimiter + %r@Ruby@ # @ as delimiter + %r!Ruby! # ! as delimiter -#### Changing the delimiter becomes useful in some cases +### Changing the delimiter becomes useful in some cases -
"http://" =~ /http:\/\//     # match http:// protocol prefix with / delimiter
-"http://" =~ %r#http://#     # match http:// protocol prefix with # delimiter
-
+ "http://" =~ /http:\/\// # match http:// protocol prefix with / delimiter + "http://" =~ %r#http://# # match http:// protocol prefix with # delimiter -#### Case sensitity +### Case sensitity -
text =~ /Ruby/                # case sensitive string matching
-text =~ /ruby/i               # case in-sensitive string matching
-
+ text =~ /Ruby/ # case sensitive string matching + text =~ /ruby/i # case in-sensitive string matching -#### Matching with wildcards +### Matching with wildcards -
"Ruby" =~ /R..y/            # match a single character with .
-"Ruby" =~ /R.*y/            # match multipe characters
-"Ruby" =~ /R[a-z]y/         # match from character range a-z
-"Rbuy" =~ /[Ruby]*/         # match from character set R,u,b and y
-"Ruby" =~ /R\wy/            # match one word character
-"regex in Ruby" =~ /\bRuby\b/"   # match the word "Ruby", but not "Ruby" as larger string
-
+ "Ruby" =~ /R..y/ # match a single character with . + "Ruby" =~ /R.*y/ # match multipe characters + "Ruby" =~ /R[a-z]y/ # match from character range a-z + "Rbuy" =~ /[Ruby]*/ # match from character set R,u,b and y + "Ruby" =~ /R\wy/ # match one word character + "regex in Ruby" =~ /\bRuby\b/" # match the word "Ruby", but not "Ruby" as larger string -#### Using quantifiers +### Using quantifiers -
"Ruby" =~ /[Ruby]{4}/          # match exactly 4 characters from set [Ruby]
-"Ruby" =~ /[Ruby]{4,4}/        # match exactly 4 characters from set [Ruby]
-"Ruby" =~ /[Ruby]{,3}/         # match at most 3 characters from set [Ruby]
-"Ruby" =~ /[Ruby]{3,}/         # match at least 3 characters from set [Ruby]
-
+ "Ruby" =~ /[Ruby]{4}/ # match exactly 4 characters from set [Ruby] + "Ruby" =~ /[Ruby]{4,4}/ # match exactly 4 characters from set [Ruby] + "Ruby" =~ /[Ruby]{,3}/ # match at most 3 characters from set [Ruby] + "Ruby" =~ /[Ruby]{3,}/ # match at least 3 characters from set [Ruby] -### 3\. Replacing Patterns +## 3. Replacing Patterns -You need to do substitution using the in place string object methods sub!() for replacing first occurence and gsub!() for replacing all occurences: +You need to do substitution using the in place string object methods `sub!()` for replacing first occurence and `gsub!()` for replacing all occurences: -
text.sub!(/Rbuy/, "Ruby")
+ text.sub!(/Rbuy/, "Ruby") -### 4\. Capture Groups +## 4. Capture Groups To extract data using regular expression we have to use capture/grouping syntax and -* **to do exactly one match:** the String#match method and MatchData#captures to produce a result array -* **to do multiple matches:** the String#scan method which returns a nested array with an result array for each match +- **to do exactly one match:** the String#match method and MatchData#captures to produce a result array +- **to do multiple matches:** the String#scan method which returns a nested array with an result array for each match + +Some basic examples to do the exactly one match with `String#match` + +Extract everything after the literal "START" + + if result = line.match(/START(.*)/") + text = result.captures + end -Some basic examples to do the exactly one match with String#match +Extract the number from a date string "2012-10-20" -
# Extract everything after the literal "START"
-if result = line.match(/START(.*)/")
-   text = result.captures
-end
-
+ if result = line.match(/(\d{4})-(\d{2})-(\d{2})/) + year, week, day = result.captures + end -
# Extract the number from a date string "2012-10-20"
-if result = line.match(/(\d{4})-(\d{2})-(\d{2})/)
-  year, week, day = result.captures
-end
-
+Nesting of capture groups, extract full name, and both parts... string "name is Doe, John" -
# Nesting of capture groups, extract full name, and both parts... string "name is Doe, John"
-if result = line.match(/name is ((\w+), (\w+))/)
-  fullname, firstname, lastname = result.capture
-end
-
+ if result = line.match(/name is ((\w+), (\w+))/) + fullname, firstname, lastname = result.capture + end Ensure always to check for nil result if the match might fail!