Beginner in regular expression












-1















def showRE(a,re)
if a =~ re
"#{$`}<<#{$&}>>#{$'}"
else
"no match"
end
end

showRE('He said "Hello"', /(["']).*?1/)
#=> "He said <<"Hello">>"


Can someone please explain why this function returns "Hello". More specifically, the purpose of *? and how that results in function returning what it does. I know ["'] finds either '/"' or '/'' and 1 refers to the match of the first group. However shouldn't that return -> 'He said Hello"' since '/'' is the first line of the string that represents what is in the brackets?










share|improve this question

























  • This indentation is really anarchy. Can you clean that up? It helps us understand what you're trying to do.

    – tadman
    Nov 14 '18 at 20:43






  • 1





    A good regular expression explainer can help illuminate what that regular expression is doing. Barring that, a good primer book is a good way to go.

    – tadman
    Nov 14 '18 at 20:45
















-1















def showRE(a,re)
if a =~ re
"#{$`}<<#{$&}>>#{$'}"
else
"no match"
end
end

showRE('He said "Hello"', /(["']).*?1/)
#=> "He said <<"Hello">>"


Can someone please explain why this function returns "Hello". More specifically, the purpose of *? and how that results in function returning what it does. I know ["'] finds either '/"' or '/'' and 1 refers to the match of the first group. However shouldn't that return -> 'He said Hello"' since '/'' is the first line of the string that represents what is in the brackets?










share|improve this question

























  • This indentation is really anarchy. Can you clean that up? It helps us understand what you're trying to do.

    – tadman
    Nov 14 '18 at 20:43






  • 1





    A good regular expression explainer can help illuminate what that regular expression is doing. Barring that, a good primer book is a good way to go.

    – tadman
    Nov 14 '18 at 20:45














-1












-1








-1








def showRE(a,re)
if a =~ re
"#{$`}<<#{$&}>>#{$'}"
else
"no match"
end
end

showRE('He said "Hello"', /(["']).*?1/)
#=> "He said <<"Hello">>"


Can someone please explain why this function returns "Hello". More specifically, the purpose of *? and how that results in function returning what it does. I know ["'] finds either '/"' or '/'' and 1 refers to the match of the first group. However shouldn't that return -> 'He said Hello"' since '/'' is the first line of the string that represents what is in the brackets?










share|improve this question
















def showRE(a,re)
if a =~ re
"#{$`}<<#{$&}>>#{$'}"
else
"no match"
end
end

showRE('He said "Hello"', /(["']).*?1/)
#=> "He said <<"Hello">>"


Can someone please explain why this function returns "Hello". More specifically, the purpose of *? and how that results in function returning what it does. I know ["'] finds either '/"' or '/'' and 1 refers to the match of the first group. However shouldn't that return -> 'He said Hello"' since '/'' is the first line of the string that represents what is in the brackets?







ruby-on-rails ruby regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:14









Cary Swoveland

69.6k54166




69.6k54166










asked Nov 14 '18 at 19:19









LawleyendaLawleyenda

359




359













  • This indentation is really anarchy. Can you clean that up? It helps us understand what you're trying to do.

    – tadman
    Nov 14 '18 at 20:43






  • 1





    A good regular expression explainer can help illuminate what that regular expression is doing. Barring that, a good primer book is a good way to go.

    – tadman
    Nov 14 '18 at 20:45



















  • This indentation is really anarchy. Can you clean that up? It helps us understand what you're trying to do.

    – tadman
    Nov 14 '18 at 20:43






  • 1





    A good regular expression explainer can help illuminate what that regular expression is doing. Barring that, a good primer book is a good way to go.

    – tadman
    Nov 14 '18 at 20:45

















This indentation is really anarchy. Can you clean that up? It helps us understand what you're trying to do.

– tadman
Nov 14 '18 at 20:43





This indentation is really anarchy. Can you clean that up? It helps us understand what you're trying to do.

– tadman
Nov 14 '18 at 20:43




1




1





A good regular expression explainer can help illuminate what that regular expression is doing. Barring that, a good primer book is a good way to go.

– tadman
Nov 14 '18 at 20:45





A good regular expression explainer can help illuminate what that regular expression is doing. Barring that, a good primer book is a good way to go.

– tadman
Nov 14 '18 at 20:45












1 Answer
1






active

oldest

votes


















3














The regular expression being passed as an argument (which I'll write in "free-spacing" mode, to make it self-documenting), is as follows:



r = /
( # start capture group 1
["'] # match a double or single parenthesis (a "character class")
) # end capture group 1
.* # match zero or more (`*`) characters (any characters)
? # make the foregoing match (.*) lazy
1 # match the contents of capture group 1
/x # free-spacing regex definition mode

str = 'He said "Hello"'
#=> "He said "Hello""
str =~ r
#=> 8 (we have a match beginning at str[8])


As str =~ r is "truthy", we evaluate



"#{$`}<<#{$&}>>#{$'}"
=> "He said <<"Hello">>"


The key here is that there are three global variables in this expression:



$` #=> "He said "
$& #=> ""Hello""
$' #=> ""


The meanings of these variables are given in this doc. You will see that:




  • $` contains the string to the left of the last successful match;

  • $& contains the string matched by the last successful match; and

  • $' contains the string to the right of the last successful match.


So we have (and return)



"#{"He said "}<<#{""Hello""}>>#{""}"
#=> => "He said <<"Hello">>"


We could alternatively use the class method Regexp::last_match:



last_match = Regexp.last_match
#=> #<MatchData ""Hello"" 1:""">


last_match is an instance of the class MatchData. That class contains many useful methods, including ones that return the values of the three global variables mentioned above:



last_match.pre_match  #=> "He said "
last_match[0] #=> ""Hello""
last_match.post_match #=> ""


I cannot say why the match .* in the regular expression was made lazy (by making it .*?).






share|improve this answer


























  • Really Impressive

    – Mayur Shah
    Nov 15 '18 at 5:45













  • Thanks a bunch!

    – Lawleyenda
    Nov 15 '18 at 14:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307360%2fbeginner-in-regular-expression%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














The regular expression being passed as an argument (which I'll write in "free-spacing" mode, to make it self-documenting), is as follows:



r = /
( # start capture group 1
["'] # match a double or single parenthesis (a "character class")
) # end capture group 1
.* # match zero or more (`*`) characters (any characters)
? # make the foregoing match (.*) lazy
1 # match the contents of capture group 1
/x # free-spacing regex definition mode

str = 'He said "Hello"'
#=> "He said "Hello""
str =~ r
#=> 8 (we have a match beginning at str[8])


As str =~ r is "truthy", we evaluate



"#{$`}<<#{$&}>>#{$'}"
=> "He said <<"Hello">>"


The key here is that there are three global variables in this expression:



$` #=> "He said "
$& #=> ""Hello""
$' #=> ""


The meanings of these variables are given in this doc. You will see that:




  • $` contains the string to the left of the last successful match;

  • $& contains the string matched by the last successful match; and

  • $' contains the string to the right of the last successful match.


So we have (and return)



"#{"He said "}<<#{""Hello""}>>#{""}"
#=> => "He said <<"Hello">>"


We could alternatively use the class method Regexp::last_match:



last_match = Regexp.last_match
#=> #<MatchData ""Hello"" 1:""">


last_match is an instance of the class MatchData. That class contains many useful methods, including ones that return the values of the three global variables mentioned above:



last_match.pre_match  #=> "He said "
last_match[0] #=> ""Hello""
last_match.post_match #=> ""


I cannot say why the match .* in the regular expression was made lazy (by making it .*?).






share|improve this answer


























  • Really Impressive

    – Mayur Shah
    Nov 15 '18 at 5:45













  • Thanks a bunch!

    – Lawleyenda
    Nov 15 '18 at 14:02
















3














The regular expression being passed as an argument (which I'll write in "free-spacing" mode, to make it self-documenting), is as follows:



r = /
( # start capture group 1
["'] # match a double or single parenthesis (a "character class")
) # end capture group 1
.* # match zero or more (`*`) characters (any characters)
? # make the foregoing match (.*) lazy
1 # match the contents of capture group 1
/x # free-spacing regex definition mode

str = 'He said "Hello"'
#=> "He said "Hello""
str =~ r
#=> 8 (we have a match beginning at str[8])


As str =~ r is "truthy", we evaluate



"#{$`}<<#{$&}>>#{$'}"
=> "He said <<"Hello">>"


The key here is that there are three global variables in this expression:



$` #=> "He said "
$& #=> ""Hello""
$' #=> ""


The meanings of these variables are given in this doc. You will see that:




  • $` contains the string to the left of the last successful match;

  • $& contains the string matched by the last successful match; and

  • $' contains the string to the right of the last successful match.


So we have (and return)



"#{"He said "}<<#{""Hello""}>>#{""}"
#=> => "He said <<"Hello">>"


We could alternatively use the class method Regexp::last_match:



last_match = Regexp.last_match
#=> #<MatchData ""Hello"" 1:""">


last_match is an instance of the class MatchData. That class contains many useful methods, including ones that return the values of the three global variables mentioned above:



last_match.pre_match  #=> "He said "
last_match[0] #=> ""Hello""
last_match.post_match #=> ""


I cannot say why the match .* in the regular expression was made lazy (by making it .*?).






share|improve this answer


























  • Really Impressive

    – Mayur Shah
    Nov 15 '18 at 5:45













  • Thanks a bunch!

    – Lawleyenda
    Nov 15 '18 at 14:02














3












3








3







The regular expression being passed as an argument (which I'll write in "free-spacing" mode, to make it self-documenting), is as follows:



r = /
( # start capture group 1
["'] # match a double or single parenthesis (a "character class")
) # end capture group 1
.* # match zero or more (`*`) characters (any characters)
? # make the foregoing match (.*) lazy
1 # match the contents of capture group 1
/x # free-spacing regex definition mode

str = 'He said "Hello"'
#=> "He said "Hello""
str =~ r
#=> 8 (we have a match beginning at str[8])


As str =~ r is "truthy", we evaluate



"#{$`}<<#{$&}>>#{$'}"
=> "He said <<"Hello">>"


The key here is that there are three global variables in this expression:



$` #=> "He said "
$& #=> ""Hello""
$' #=> ""


The meanings of these variables are given in this doc. You will see that:




  • $` contains the string to the left of the last successful match;

  • $& contains the string matched by the last successful match; and

  • $' contains the string to the right of the last successful match.


So we have (and return)



"#{"He said "}<<#{""Hello""}>>#{""}"
#=> => "He said <<"Hello">>"


We could alternatively use the class method Regexp::last_match:



last_match = Regexp.last_match
#=> #<MatchData ""Hello"" 1:""">


last_match is an instance of the class MatchData. That class contains many useful methods, including ones that return the values of the three global variables mentioned above:



last_match.pre_match  #=> "He said "
last_match[0] #=> ""Hello""
last_match.post_match #=> ""


I cannot say why the match .* in the regular expression was made lazy (by making it .*?).






share|improve this answer















The regular expression being passed as an argument (which I'll write in "free-spacing" mode, to make it self-documenting), is as follows:



r = /
( # start capture group 1
["'] # match a double or single parenthesis (a "character class")
) # end capture group 1
.* # match zero or more (`*`) characters (any characters)
? # make the foregoing match (.*) lazy
1 # match the contents of capture group 1
/x # free-spacing regex definition mode

str = 'He said "Hello"'
#=> "He said "Hello""
str =~ r
#=> 8 (we have a match beginning at str[8])


As str =~ r is "truthy", we evaluate



"#{$`}<<#{$&}>>#{$'}"
=> "He said <<"Hello">>"


The key here is that there are three global variables in this expression:



$` #=> "He said "
$& #=> ""Hello""
$' #=> ""


The meanings of these variables are given in this doc. You will see that:




  • $` contains the string to the left of the last successful match;

  • $& contains the string matched by the last successful match; and

  • $' contains the string to the right of the last successful match.


So we have (and return)



"#{"He said "}<<#{""Hello""}>>#{""}"
#=> => "He said <<"Hello">>"


We could alternatively use the class method Regexp::last_match:



last_match = Regexp.last_match
#=> #<MatchData ""Hello"" 1:""">


last_match is an instance of the class MatchData. That class contains many useful methods, including ones that return the values of the three global variables mentioned above:



last_match.pre_match  #=> "He said "
last_match[0] #=> ""Hello""
last_match.post_match #=> ""


I cannot say why the match .* in the regular expression was made lazy (by making it .*?).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 8:15

























answered Nov 15 '18 at 5:40









Cary SwovelandCary Swoveland

69.6k54166




69.6k54166













  • Really Impressive

    – Mayur Shah
    Nov 15 '18 at 5:45













  • Thanks a bunch!

    – Lawleyenda
    Nov 15 '18 at 14:02



















  • Really Impressive

    – Mayur Shah
    Nov 15 '18 at 5:45













  • Thanks a bunch!

    – Lawleyenda
    Nov 15 '18 at 14:02

















Really Impressive

– Mayur Shah
Nov 15 '18 at 5:45







Really Impressive

– Mayur Shah
Nov 15 '18 at 5:45















Thanks a bunch!

– Lawleyenda
Nov 15 '18 at 14:02





Thanks a bunch!

– Lawleyenda
Nov 15 '18 at 14:02




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307360%2fbeginner-in-regular-expression%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python