# What is a regular expression?

It is one of the methods to express a set of character strings with one character string. Regular expressions use special characters called "metacharacters". (By the way, all are half-width.)

## Types and usage of metacharacters

--Any single character "." --By using the half-width ".", You can express any single character.

-Example-

``````
I.is
#=>I'm a bird, I'm a horse, I'm 0...etc
``````

--Beginning and end of line "^" "\$" --You can search for strings that exist only at the beginning or end of a line.

-Example-

``````
^Thank you\$
#=>Matches lines with only the words "Thank you"
``````

--Repeat of the same character "" "+" "?" ―― “” means that there is no previous character or that the previous character is one or more consecutive. ―― “+” means that at least one character must be immediately before the +, and then it is continuous. ―― “?” Means that there is no previous character or only one character.

-Example-

``````
Oh*I
#=>Hey, hey, hey...etc

Oh+I
#=>Hey, hey, hey...etc

Oh?I
#=>Hey, hey...etc
``````

--Any character string "|" -Matches a regular expression when any of the strings separated by | exists.

-Example-

``````
ＩＢＭ|Microsoft|Ａｐｐｌｅ|Netscape
#=>IBM Microsoft Apple Netscape
``````

--Any of the specified characters "[]" -The characters enclosed in [] match any one of them. -In [], metacharacters are recognized as ordinary characters.

-Example-

``````
Ａ[A-Z]ＣＣＣ
#=>Ａ[ABCDEFGHIJKLMNOPQRSTUVWXYZ]ＣＣＣ

Ａ[0-9]ＣＣＣ
#=>Ａ[0123456789]ＣＣＣ

Ａ[A-Z0-9]ＣＣＣ
#=>Search for strings that start with A and end with CCC, where the next letter is a half-width uppercase alphabet or number
``````

--Grouping "()" --Can be processed in batches of several characters.

-Example-

``````
(See you)+Hmm
#=>Okay, okay, okay, okay...etc

tomorrow(I|I)Go home
#=>I'm going home tomorrow, I'm going home tomorrow...etc
``````

## List of frequently used regular expressions

``````
#All numerical values (full-width)
/\A[０-９]+\z/

#All numerical values (half-width)
/\A[0-9]+\z/

#All numerical values (full-width,Half size)
/\A[0-9０-９]+\z/

#Half-width alphabet (lowercase)
/\A[a-z]+\z/

#Half-width alphabet (uppercase)
/\A[A-Z]+\z/

#Half-width alphabet (uppercase / lowercase)
/\A[a-zA-Z]+\z/

#Half-width alphabet (lowercase / numerical)
/\A[a-z0-9]+\z/

#Half-width alphabet (uppercase / numerical)
/\A[A-Z0-9]+\z/

#Half-width alphabet (uppercase / lowercase / numerical value)
/\A[a-zA-Z0-9]+\z/

#Full-width hiragana
/\A[Ah-Hmm-]+\z/

#Full-width katakana
/\A[A-Car-]+\z/

#Full-width hiragana, katakana
/\A[Ah-Hmm-Car-]+\z/

#Half-width kana
/\A[ｧ-N]+\z/

#Zip code (3 digits, 5 digits, 7 digits with hyphen)
/\A\d{3}-\d{4}\$|^\d{3}-\d{2}\$|^\d{3}\z/

#Zip code (5 digits with hyphen)
/\A\d{3}-\d{2}\z/

#Zip code (7 digits with hyphen)
/\A\d{3}-\d{4}\z/

#Zip code (3 digits without hyphens)
/\A\d{3}\z/

#Zip code (5 digits without hyphens)
/\A\d{5}\z/

#Zip code (7 digits without hyphens)
/\A\d{7}\z/

#phone number(10 digits without hyphens)
/\A\d{10}\z/

#Cellphone number(11 digits without hyphens)
/\A\d{11}\z/

#Cellphone number(10 digits or 11 digits without hyphens)
/\A\d{10,11}\z/

# e-mail
/\A\S+@\S+\.\S+\z/

# URL
/\Ahttps?:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?/

#credit card(VISA,Master,Diners,Discover,Amex compatible)
/\A(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})\z/
``````

## reference

Introduction to regular expressions that monkeys can understand

Ruby Association: Regular Expression

Regular expressions for validation of Ruby