简体   繁体   中英

Ruby regular expressions and bracket. What do the brackets do?

I am going through the Peter Cooper book "Beginning Ruby" and I have some questions regarding some of the string methods and regular expression usage. I think I'm clear on what a regular expression is: "a string that describes a pattern for matching elements in other strings."

So:

"This is a test".scan(/\w\w/) {|x| puts x}

Output:
Th
is
is
te
st
=> "This is a test"
  1. So it prints two characters at a time. I didn't realize it also returns the original string. Why is this?

Also,

    "This is a test".scan(/[aeiou]/) { |x| puts x }
  1. What do the brackets do? I think they are called character classes, but I am not sure exactly what they do. The explanation in Cooper's book isn't totally verbose and clear.

Explanation of character classes:

"The last important aspect of regular expressions you need to understand at this stage is character classes. These allow you to match against a specific set of characters. For example, you can scan through all the vowels in a string:"

Yes, it is called a character class .

A character class defines a set of characters. Saying, "match one character specified by the class". The two implementations of a character class are considered a positive class [ ] and a negative class [^ ] . The positive character class allows you to define a list of characters, any one of which may appear in a string for a match to occur while the negative class allows you to define a list of characters that must NOT appear in a string for a match to occur.

Explanation of your character class:

[aeiou]    # any character of: 'a', 'e', 'i', 'o', 'u'
  1. The scan method usually returns an array with the matches, but it optionally accepts a block, which is equivalent to do an each of the resulting array.

    Here is the documentation: http://www.ruby-doc.org/core-2.1.3/String.html#method-i-scan

  2. To the second question, @hwnd already gave you a clear answer. The best way to learn this is to experiment, regex101.com is the online tool I usually use. It lists explanations for all your matching elements, so it's a wonderful learning resource too.

    Some things you might like to try:

    1. 123abab12ab1234 with pattern [123]
    2. 123abab12ab1234 with pattern [ab]+
    3. 123abab12ab1234 with pattern b[1|a]

One thing to remember is that a character class matches ONE character, for example:

str = 'XXXaeiouXXX'
puts str
str.sub!(/[aeiou]/, '.')
puts str

--output:--
XXXaeiouXXX
XXX.eiouXXX

A character class says, "Match this character OR this character OR this character...ONE TIME ".

Also check out rubular:

http://rubular.com/

I didn't realize it also returns the original string. Why is this?

So that you can chain methods together:

my_str.scan(...).downcase.capitalize.each_char {|char| puts char}.upcase.chomp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM