Notes on regular expressions

Regular expressions

A regular expression is an expression method for extracting and replacing a part of a character string and checking whether the character string satisfies the constraint.

Scene with regular expressions In the case of the field to enter the telephone number on the registration form. Some people type with a hyphen, while others type without a hyphen. In order to unify the format, if there is a hyphen in the entered phone number, it will be processed as necessary.

Input field:090-1234-5678

⇓ Remove hyphens

    09012345678

In this way, the technique for checking whether a specific character is included in a character string and performing operations such as removing a specific character is called ** regular expression **.


Two methods for using regular expressions

◾ sub method ◾ match method

** ◾ sub ** The sub method is a method for replacing the specified part of a character string with another character string. Specify the character string to be replaced in the first argument, and specify the converted character string in the second argument. Also, enclose the character string you want to operate in /.

** (Example) ** Replaced the string "eat bread" with the string "eat rice".

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

irb(main):002:0> str.sub(/Bread/,"rice")
=> "eat rice"


** ◾ match ** <Extraction / constraint survey> The match method is a method to check whether the character string specified in the argument is included in the character string using the method. If it is included, the specified string will be returned as an object called MatchData. If it is not included, the return value is nil.

** (Example) ** Check if the character string "Good morning" contains the character string "Good"

irb(main):001:0> str = "Good morning"
=> "Good morning"

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

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

MatchData The matched string is returned as an object of type MatchData. If you want to extract the character string that actually matches from MatchData, you can extract it in the same way as when extracting the first data from the array.

irb(main):001:0> str = "good morning"
=> "good morning"

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

irb(main):003:0> md[0]
=> "good"

Recommended Posts

Notes on regular expressions
Regular expressions
Notes on how to use regular expressions in Java
python notes on docker
[Android] Notes on xml
Notes on multiple inheritance
colorize and regular expressions
[Java] Summary of regular expressions
About regular expressions in Ruby
[Ruby] Notes on gets method
Learn regular expressions little by little ①
Notes on Protocol Buffers
python notes on docker
[Android] Notes on xml
Notes on multiple inheritance
Notes on regular expressions
Rails: Capture regular expressions in emails!
Parse Japanese addresses with regular expressions
Notes on signal control in Java
Notes on calling Installer on Android App
Implementation of validation using regular expressions
Notes on migrating from CircleCI 1.0 to 2.0
Notes on Android (java) thread processing
Notes on Java path and Package
Name a group of regular expressions (Java)
Easily make troublesome regular expressions with Rubular
Notes on operators using Java ~ String type ~
Notes on expand () and collapse () of Expandablerecyclerview
Notes on creating android plugins for Unity
Notes on using FCM with Ruby on Rails
Regular expressions that match 99% of email addresses
Notes on Java's Stream API and SQL
Notes on JSP extension tags in SpringFrameWork
Easy to trip with Java regular expressions