About regular expressions in Ruby

About Ruby regular expressions Regarding the regular expressions I learned in the curriculum the other day, I was in a state of "a lot of codes are written ...", so I wrote an article that also serves as "organization of what I learned" and "learning output".

-

What is a regular expression? </ h3> It is one of the methods to express a set of character strings with one character string. It is also called a regular expression, and this translation is relatively used in the field of formal language theory. In rare cases, it is also called a regular expression. (Excerpt from wikipedia https://ja.wikipedia.org/wiki/%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%8F%BE)

It's hard to understand if it's just this, so when I touch the application, there are many things like "If the password is less than 6 characters, you can not register" or "If the phone number contains a hyphen ...". So how do you make this decision?

For example, when registering a phone number

id name tel
1 Mr. A 090-0000-0000
2 Mr. B 09000000000

Mr. A → Register with hyphen Mr. B → Register without hyphen

Such a situation is born. If this is the case, it will be managed in a different format, so it is easier to manage if it is unified. At that time, "Erase the extra hyphen! ], It will be easier to manage. In this way, "Does the string contain a specific character?" , And the technology that performs processing such as "remove specific characters" is a regular expression. Regular expressions can be represented in various languages with similar code, so once you remember the shape, you can apply it to other languages.

-

Regular expression method </ h3>

In Ruby, the following three methods are mainly important and often used.

sub method This method replaces the specified part of a character string with another character string. For example, use the code below.
 greeting = "Hello,world"
=>"Hello,world"

 greeting.sub(/Hello/,"Good morning")
=>"Good morning,world" 

I was able to replace the string "Hello" enclosed in / with "Good morning".

muchch method This method is used to determine whether the character string specified in the argument is included in the called character string.
 greeting = "Hello,world"
=>"Hello,world"

 greeting.mutch(/Hello/)
=> #<MatchData "Hello">

greeting.match(/Good morning/)
=> nil 

As mentioned above, the character string assigned to greeting is "Hello, world", so if the argument is specified by / Hello / in the match method, "Hello" is called as MatchDate. However, if the argument is specified as / Good morning /, it will be a character string that is not included in the assigned character string, so it will be displayed as "nil".

gsub method Similar to the sub method, it is a method for replacing a character string, but by adding g (global method), the character string specified in the character string can be added. If more than one is included, it has the role of replacing all of them. Take a phone number as an example
telA = '090-0000-0000'
=> "090-00000-0000"

telA.gsub(/-/,'')
=> "090000000000"

In this way, I was able to remove the hyphen surrounded by /. As described above, you can restrict the character string by specifying the character string to be removed or the character string to be included in the method. The description method for specifying is summarized below, so please refer to it if you have a chance to use it.

pattern meaning
[a-z] Matches any of the alphabets a through z
\d Match numbers
[a-z\d] Matches any one of the alphanumeric characters
{n,m} Matches the previous character that appears at least n times and at most m times
i Search case-insensitive (last)
. Matches any single character
+ The previous character matches one or more repetitions
\A The character immediately following matches the character string at the beginning
\z Match the previous character to the string at the end
[Ah-Hmm-One-龥] Matches kana, kana, or kanji surrounded by square brackets
?= Matches the character string immediately after the set character
*? Check the character string in which the character set immediately before continues 0 times or more,?Returns one character when the character immediately after
/\A(?=.?[a-z])(?=.?[\d])[a-z\d]+\z/i Matches any consecutive alphanumeric string starting with 0 or more arbitrary characters and ending with 1 or more alphanumeric characters, and is not case sensitive (can be used when checking mixed alphanumeric passwords, etc.) )

Recommended Posts