There is a shortest match in a regular expression, but let's check how to use it while watching the operation with some samples.
str =" Ah ah "Ah" I "Uu" Yes "Oh" Kaka? "
If you want to extract only the part enclosed in parentheses from such a character string (that is, you want to extract three places of "a", "u", and "oh"), you will use the shortest match.
** + ** does not match in this example because it matches with one or more characters.
str = "Ah ah """
puts str.scan(/「.+」/)
=>Does not match
*** matches with 0 or more characters, so it matches in this example.
str = "Ah ah """
puts str.scan(/「.*」/)
=>「」
Since there is one character ** a **, it matches.
str = "Ah ah "Ah""
puts str.scan(/「.+」/)
=>"Ah"
This also matches because there are 0 characters ** a **. The result is the same.
str = "Ah ah "Ah""
puts str.scan(/「.*」/)
=>"Ah"
In this example, the part enclosed by ** "" that appeared first and ** " that appeared last will match. This is not what I want to do.
str = "Ah ah "ah" i "uu" yeah "oh""
puts str.scan(/「.+」/)
=>"Ah" I "Uu" Yes "Oh"
**? If you add **, it will make the shortest match.
str = "Ah ah "ah" i "uu" yeah "oh""
puts str.scan(/「.+?」/)
=>"A" "Uu" "Oh"
Congratulations. : clap :: clap :: clap ::
If you use *** instead of ** + **, the result is the same, but ...
str = "Ah ah "ah" i "uu" yeah "oh""
puts str.scan(/「.*?」/)
=>"A" "Uu" "Oh"
Depending on whether you want to match ** "" ** or not, you will have to use ** + ** and *** ** properly.
If you don't want to match ** "" **, use ** + **, which matches one or more characters.
str = "Ah ah "Ah" I "Uu" Yeah "Oh" Kaka """
puts str.scan(/「.+?」/)
=>"A" "Uu" "Oh"
If you want to match ** "" **, use ***, which matches with 0 or more characters.
str = "Ah ah "Ah" I "Uu" Yeah "Oh" Kaka """
puts str.scan(/「.*?」/)
=>"A" "Uu" "Oh" ""
C'est fini :sweat_smile: