-
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.
-
In Ruby, the following three methods are mainly important and often used.
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".
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".
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