Regexp Regular Expression in Ruby Note 1

Source: Internet
Author: User
Tags character classes

 

Regular Expression

Regular Expressions are applicable in many scenarios. For example, verify whether the string matches the format, find the matched string, and replace the matched string.

Many programming languages support regular expressions with similar syntax.

A regular expression consists of two parts: Pattern and matching string.

We usually say that writing a regular expression is to write a pattern. Then verify that some input strings match this pattern.

 

Regexp

In ruby, the pattern is called Regexp. The content wrapped in/.../or % r (...) Is this Regexp.

For example

 
 
  1. /regexp/ 
  2. %r(regexp) 

Haystack contains y, so they match.

 
 
  1. /y/.match('haystack') #=> #<MatchData "y"> 

Haystack does not contain needle, so nil is returned if it does not match.

 
 
  1. /needle/.match('haystack') #=> nil 

Haystack contains hay, so it matches.

 
 
  1. /hay/.match('haystack')    #=> #<MatchData "hay"> 
 

Metacharacters and Escapes

Character(,),[,],{,},.,?,+,*They are all metacharacters. They have special meanings in the mode. If you want to match these strings, you need to add the backslash \ before them so that these special characters are escape from the mode, represents a common character.

 
 
  1. /1 \+ 2 = 3\?/.match('Does 1 + 2 = 3?') #=> #<MatchData "1 + 2 = 3?"> 

Ruby expressions can also be embedded into the pattern, as long as they are written in.

 
 
  1. Place = "Tokyo"
  2. /# {Place}/. match ("Go to Tokyo ")
  3. #=># <MatchData "Tokyo">
 

Character Classes

Use some [] characters to check whether they appear in match. /[AB]/represents a or B,/AB/represents a followed by B.

 
 
  1. /W[aeiou]rd/.match("Word") #=> #<MatchData "Word"> 

The two connected characters represent a range. [a-d] and [abcd] indicate a range.

[] Can contain multiple ranges [a-dx-z] and [abcdxyz.

 
 
  1. /[0-9a-f]/.match('9f') #=> #<MatchData "9"> 
  2. /[9f]/.match('9f')     #=> #<MatchData "9"> 

^ Indicates the opposite, except for the content after ^ In the mode.

 
 
  1. /[^a-eg-z]/.match('f') #=> #<MatchData "f"> 
  • /./, Which represents any character, except for new lines.
  • //./M, which represents any character. m indicates that multiple rows can be matched.
  • /\ W/, represents a character, [a-zA-Z0-9].
  • /\ W/, representing a non-character, [^ a-zA-Z0-9].
  • /\ D/, representing a number [0-9].
  • /\ D/, representing a non-number, [^ 0-9].
  • /\ H/, representing a hexadecimal character, [0-9a-fA-F].
  • /\ H/, which is a non-hexadecimal character [^ 0-9a-fA-F].
  • /\ S/, Represents a blank character, [\ t \ r \ n \ f]/.
  • /\ S/, Representing a non-blank character, [^ \ t \ r \ n \ f]/.

 

Repetition already exists

Repeated characters can indicate the number of repeated characters.

  • *-0 or multiple times.

  • +-Once or multiple times.

  • ?-0 or multiple times.

  • {N}-N times.

  • {N,}-N times or more.

  • {,M}-M times or less.

  • {N,M}-Minimum n times, maximum m times.

Repetition is greedy by default, and it will try its best to backward match and match more content. Lazy match only finds the most recent matching string and only matches the minimum number.

By adding? It turns greed into laziness.

 
 
  1. /<.+>/.match("<a><b>")  #=> #<MatchData "<a><b>"> 
  2. /<.+?>/.match("<a><b>") #=> #<MatchData "<a>"> 

 

 

 

References

1. Regexp

2. Regular expressions to be mastered by SEO

 

This article is from the "breakthrough IT architects" blog, please be sure to keep this source http://virusswb.blog.51cto.com/115214/1043505

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.