I want to be able to think and write regular expressions myself. ..

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-


IBM|Microsoft|Apple|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[A-Z]CCC
#=>A[ABCDEFGHIJKLMNOPQRSTUVWXYZ]CCC 

A[0-9]CCC
#=>A[0123456789]CCC

A[A-Z0-9]CCC
#=>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[0-9]+\z/

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

#All numerical values (full-width,Half size)
/\A[0-90-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

Recommended Posts

I want to be able to think and write regular expressions myself. ..
Be able to write Hamcrest Matchers in lambda expressions
I want to be able to pass items that have been validated with regular expressions even if they are left blank
I want to write a nice build.gradle
I want to write a unit test!
[Memorandum] I've started working, so I want to study regular expressions well [Regular expressions]
I want to simply write a repeating string
I want to be eventually even in kotlin
I want to write quickly from java to sqlite
I want to be able to read a file using refile with administrate [rails6]
I want to transition screens with kotlin and java!
colorize and regular expressions
I want to be aware of the contents of variables!
I want to set the conditions to be displayed in collection_check_boxes
I want to bring Tomcat to the server and start the application
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
[jackson] I want to receive JSON values "0" and "1" as boolean
When you want to explicitly write OR or AND with ransack
I want to implement various functions with kotlin and java!
I tried to be able to pass multiple objects with Ractor
I want to convert characters ...
I want to give edit and delete permissions only to the poster
Webpack and webpacker I want to tell Ruby people right now
I just want to write Java using Eclipse on my Mac
I want to return to the previous screen with kotlin and java!
I really want to do "new T ()"! (And without inspection exceptions)
I want to perform asynchronous processing and periodic execution with Rail !!!
I want to extract between character strings with a regular expression
I think it's okay to write joins etc. in the scope
Swift: I want to chain arrays
Vuze plugin to write and enjoy
I want to use FormObject well
I want to convert InputStream to String
I want to docker-compose up Next.js!
I want to write JSP in Emacs more easily than the default.
I want to display images with REST Controller of Java and Spring!
[Active Admin] I want to customize the default create and update processing
[Ruby] I want to extract only the value of the hash and only the key
I want to pass the argument of Annotation and the argument of the calling method to aspect
I will write 5 ways to implement Java Singleton and various advantages and disadvantages