Ruby regular expression

What is a regular expression?

-Replace a part of the character string -Extract a part of the character string -Check if the string meets the constraints

Regular expressions are a technique for performing operations such as.

Most descriptions of regular expressions can be shared between any languages.

Methods and usage examples

sub method

Replace the specified part of the string with another string </ font>

irb(main):001:0> str = "Eat an apple"
=> "Eat an apple"

irb(main):002:0> str.sub(/Apple/,"Orange")
=> "Eat orange"

The string "eat apple" is assigned to the variable str.

Next, for str, the sub method is used, and apple is specified as the first argument and orange is specified as the second argument.

When output, it is supposed to eat apples → eat oranges.

In this way, ** enclose the character string you want to replace with the first argument in "/ (slash)", and specify the converted character string as the second argument. ** **

match method

Check if the string is included in the string to the left of the method </ font>


irb(main):001:0> str = "Hello, World"
=> "Hello, World"

irb(main):002:0> str.match(/Hello/)
=> #<MatchData "Hello">

irb(main):003:0> str.match(/Good/)
=> nil

The string "Hello, World" is assigned to the variable str

Next, for str, the match method is used and Hello is specified as an argument.

Since "Hello, world" contains the specified string "Hello", you will get the specified string "Hello" as the return value of the MatchData object.

Since "Good" is not included, the return value will be nil.

Like the sub method, the match method also encloses the ** argument with "/ (slash)". ** **

gsub method

Replace all specified parts of the string with another string </ font>

irb(main):001:0> tel = '080-1234-5678'
=> "080-1234-5678"

irb(main):002:0> tel.sub(/-/,'')
=> "0801234-5678"

irb(main):003:0> tel.gsub(/-/,'')
=> "08012345678"

The telephone number is assigned to the variable tel.

I then try to get rid of it by replacing the hyphen with an empty string in the sub method, but only the first hyphen is replaced.

Use sub to replace only the first specified string, gsub to replace all.

Regular expression metacharacters

Metacharacters are characters with special meanings and functions, and various restrictions can be made by using them.

Since there are many types, I will introduce some of them by taking password restrictions as an example.

irb(main):001:0> pw = 'Abcd1234'
=> "Abcd1234"

irb(main):002:0> pw.match(/[a-z\d]{8,10}/i)
=> #<MatchData "Abcd1234">

The above is

・ Either letters from a to z or numbers ・ 8 to 10 characters ・ Characters can be large or small

I am putting the constraint.

I will decompose the contents of the argument and explain the meaning of each character

/[a-z\d]{8,10}/i

/ [a-z \ d] {8,10} / i </ font> (The character string in the slash can be either uppercase or lowercase)

[a-z \ d] {8,10} </ font> (matches the one in which the previous character appears 8 to 10 characters)

[a-z] \ d </ font> (matches the numbers)

[a-z] </ font> (matches any of the letters a to z)

  • This time, in addition to a-z, the numbers are also enclosed in square brackets, so they match any of the alphanumeric characters.)

Summary

type meaning
[ ] Match any one of the enclosed characters
\d Match numbers
{a,b} Matches the previous character that appears a or more and b or less
i (option) Case insensitive

Various constraints can be implemented by combining the two methods introduced basically with the regular expression pattern.

The pattern is just an example. There are many other things you can do with regular expressions.

Recommended Posts