Ruby regular expression non capture group











up vote
3
down vote

favorite
2












I'm trying to grab id number from the string, say



id/number/2000GXZ2/ref=sr


using



(?:id/number/)([a-zA-Z0-9]{8})


for some reason non capture group is not worked, giving me:



id/number/2000GXZ2









share|improve this question




















  • 2




    How are you getting the result? You need to get the first captured group
    – Tushar
    Feb 17 '16 at 16:16






  • 4




    The purpose of a non-capturing group is to group things without generating a capture, but not to remove the matched substring from the global match.
    – Casimir et Hippolyte
    Feb 17 '16 at 16:16






  • 1




    It is working if you properly fetch rubular.com/r/6NcnBGTEwy
    – Nermin
    Feb 17 '16 at 16:17








  • 1




    A regex does not do anything by itself. How are you using the regex? Splitting the string with it? Substituting some parts of the string with it?
    – sawa
    Feb 17 '16 at 16:22












  • What exactly do you need to get of this: id/number/2000GXZ2/ref=sr? (which part)
    – Shafizadeh
    Feb 17 '16 at 16:23















up vote
3
down vote

favorite
2












I'm trying to grab id number from the string, say



id/number/2000GXZ2/ref=sr


using



(?:id/number/)([a-zA-Z0-9]{8})


for some reason non capture group is not worked, giving me:



id/number/2000GXZ2









share|improve this question




















  • 2




    How are you getting the result? You need to get the first captured group
    – Tushar
    Feb 17 '16 at 16:16






  • 4




    The purpose of a non-capturing group is to group things without generating a capture, but not to remove the matched substring from the global match.
    – Casimir et Hippolyte
    Feb 17 '16 at 16:16






  • 1




    It is working if you properly fetch rubular.com/r/6NcnBGTEwy
    – Nermin
    Feb 17 '16 at 16:17








  • 1




    A regex does not do anything by itself. How are you using the regex? Splitting the string with it? Substituting some parts of the string with it?
    – sawa
    Feb 17 '16 at 16:22












  • What exactly do you need to get of this: id/number/2000GXZ2/ref=sr? (which part)
    – Shafizadeh
    Feb 17 '16 at 16:23













up vote
3
down vote

favorite
2









up vote
3
down vote

favorite
2






2





I'm trying to grab id number from the string, say



id/number/2000GXZ2/ref=sr


using



(?:id/number/)([a-zA-Z0-9]{8})


for some reason non capture group is not worked, giving me:



id/number/2000GXZ2









share|improve this question















I'm trying to grab id number from the string, say



id/number/2000GXZ2/ref=sr


using



(?:id/number/)([a-zA-Z0-9]{8})


for some reason non capture group is not worked, giving me:



id/number/2000GXZ2






ruby regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 17 hours ago









Nakilon

26.3k1283104




26.3k1283104










asked Feb 17 '16 at 16:14









AKarpun

61211




61211








  • 2




    How are you getting the result? You need to get the first captured group
    – Tushar
    Feb 17 '16 at 16:16






  • 4




    The purpose of a non-capturing group is to group things without generating a capture, but not to remove the matched substring from the global match.
    – Casimir et Hippolyte
    Feb 17 '16 at 16:16






  • 1




    It is working if you properly fetch rubular.com/r/6NcnBGTEwy
    – Nermin
    Feb 17 '16 at 16:17








  • 1




    A regex does not do anything by itself. How are you using the regex? Splitting the string with it? Substituting some parts of the string with it?
    – sawa
    Feb 17 '16 at 16:22












  • What exactly do you need to get of this: id/number/2000GXZ2/ref=sr? (which part)
    – Shafizadeh
    Feb 17 '16 at 16:23














  • 2




    How are you getting the result? You need to get the first captured group
    – Tushar
    Feb 17 '16 at 16:16






  • 4




    The purpose of a non-capturing group is to group things without generating a capture, but not to remove the matched substring from the global match.
    – Casimir et Hippolyte
    Feb 17 '16 at 16:16






  • 1




    It is working if you properly fetch rubular.com/r/6NcnBGTEwy
    – Nermin
    Feb 17 '16 at 16:17








  • 1




    A regex does not do anything by itself. How are you using the regex? Splitting the string with it? Substituting some parts of the string with it?
    – sawa
    Feb 17 '16 at 16:22












  • What exactly do you need to get of this: id/number/2000GXZ2/ref=sr? (which part)
    – Shafizadeh
    Feb 17 '16 at 16:23








2




2




How are you getting the result? You need to get the first captured group
– Tushar
Feb 17 '16 at 16:16




How are you getting the result? You need to get the first captured group
– Tushar
Feb 17 '16 at 16:16




4




4




The purpose of a non-capturing group is to group things without generating a capture, but not to remove the matched substring from the global match.
– Casimir et Hippolyte
Feb 17 '16 at 16:16




The purpose of a non-capturing group is to group things without generating a capture, but not to remove the matched substring from the global match.
– Casimir et Hippolyte
Feb 17 '16 at 16:16




1




1




It is working if you properly fetch rubular.com/r/6NcnBGTEwy
– Nermin
Feb 17 '16 at 16:17






It is working if you properly fetch rubular.com/r/6NcnBGTEwy
– Nermin
Feb 17 '16 at 16:17






1




1




A regex does not do anything by itself. How are you using the regex? Splitting the string with it? Substituting some parts of the string with it?
– sawa
Feb 17 '16 at 16:22






A regex does not do anything by itself. How are you using the regex? Splitting the string with it? Substituting some parts of the string with it?
– sawa
Feb 17 '16 at 16:22














What exactly do you need to get of this: id/number/2000GXZ2/ref=sr? (which part)
– Shafizadeh
Feb 17 '16 at 16:23




What exactly do you need to get of this: id/number/2000GXZ2/ref=sr? (which part)
– Shafizadeh
Feb 17 '16 at 16:23












3 Answers
3






active

oldest

votes

















up vote
8
down vote



accepted










As mentioned by others, non-capturing groups still count towards the overall match. If you don't want that part in your match use a lookbehind.
Rubular example



(?<=id/number/)([a-zA-Z0-9]{8})



(?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text




Ruby Doc Regexp



Also, the capture group around the id number is unnecessary in this case.






share|improve this answer





















  • Thank you a lot!
    – AKarpun
    Feb 20 '16 at 20:26


















up vote
2
down vote













You have:



str = "id/number/2000GXZ2/ref=sr"

r = /
(?:id/number/) # match string in a non-capture group
([a-zA-Z0-9]{8}) # match character in character class 8 times, in capture group 1
/x # extended/free-spacing regex definition mode


Then (using String#):



str[r]
#=> "id/number/2000GXZ2"


returns the entire match, as it should, not just the contents of capture group 1. There are a few ways to remedy this. Consider first ones that do not use a capture group.



@jacob.m suggested putting the first part in a positive lookbehind (modified slightly from his code):



r = /
(?<=id/number/) # match string in positive lookbehind
[[:alnum:]]{8} # match >= 1 alphameric characters
/x

str[r]
#=> "2000GXZ2"


An alternative is:



r = /
id/number/ # match string
K # forget everything matched so far
[[:alnum:]]{8} # match 8 alphanumeric characters
/x

str[r]
#=> "2000GXZ2"


K is especially useful when the match to forget is variable-length, as (in Ruby) positive lookbehinds do not work with variable-length matches.



With both of these approaches, if the part to be matched contains only numbers and capital letters, you may want to use [A-Z0-9]+ instead of [[:alnum:]]. In fact, if all the entries have the form of your example, you might be able to use:



r = /
d # match a digit
[A-Z0-9]{7} # match >= 0 capital letters or digits
/x

str[r]
#=> "2000GXZ2"


If the substring to be matched is always followed by a non-alphanumeric character, you could replace {8} with +, {7} with *.



The other line of approach is to keep your capture group. One simple way is:



r = /
id/number/ # match string
([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
/x

str =~ r
$1 #=> "2000GXZ2"


Alternatively, you could use String#sub to replace the entire string with the contents of the capture group:



r = /
id/number/ # match string
([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
.* # match the remainder of the string
/x

str.sub(r, '1') #=> "2000GXZ2"
str.sub(r, "\1") #=> "2000GXZ2"
str.sub(r) { $1 } #=> "2000GXZ2"





share|improve this answer























  • Wow, thank you for such thoroughful answer. Much apreciate it !
    – AKarpun
    Feb 20 '16 at 20:25


















up vote
0
down vote













This is Ruby Regexp expected match consistency evilness. Some Regexp-style methods will return the global-match while others will return specified matches.



In this case, one method we can use to get the behavior you're looking for is scan.



I don't think anyone here actually mentions how to get your Regexp working as you originally intended, which was to get the capture-only match. To do that, you would use the scan method like so with your original pattern:



test_me.rb



test_string="id/number/2000GXZ2/ref=sr"
result = test_string.scan(/(?:id/number/)([a-zA-Z0-9]{8})/)
puts result



2000GXZ2



That said, replacing (?:) with (?<=) for non-capture groups for look-behinds will benefit you both when you use scan as well as other parts of ruby that use Regexps.






share|improve this answer





















    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',
    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%2f35462038%2fruby-regular-expression-non-capture-group%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    8
    down vote



    accepted










    As mentioned by others, non-capturing groups still count towards the overall match. If you don't want that part in your match use a lookbehind.
    Rubular example



    (?<=id/number/)([a-zA-Z0-9]{8})



    (?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text




    Ruby Doc Regexp



    Also, the capture group around the id number is unnecessary in this case.






    share|improve this answer





















    • Thank you a lot!
      – AKarpun
      Feb 20 '16 at 20:26















    up vote
    8
    down vote



    accepted










    As mentioned by others, non-capturing groups still count towards the overall match. If you don't want that part in your match use a lookbehind.
    Rubular example



    (?<=id/number/)([a-zA-Z0-9]{8})



    (?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text




    Ruby Doc Regexp



    Also, the capture group around the id number is unnecessary in this case.






    share|improve this answer





















    • Thank you a lot!
      – AKarpun
      Feb 20 '16 at 20:26













    up vote
    8
    down vote



    accepted







    up vote
    8
    down vote



    accepted






    As mentioned by others, non-capturing groups still count towards the overall match. If you don't want that part in your match use a lookbehind.
    Rubular example



    (?<=id/number/)([a-zA-Z0-9]{8})



    (?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text




    Ruby Doc Regexp



    Also, the capture group around the id number is unnecessary in this case.






    share|improve this answer












    As mentioned by others, non-capturing groups still count towards the overall match. If you don't want that part in your match use a lookbehind.
    Rubular example



    (?<=id/number/)([a-zA-Z0-9]{8})



    (?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text




    Ruby Doc Regexp



    Also, the capture group around the id number is unnecessary in this case.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 17 '16 at 16:52









    jacob.mccrumb

    379113




    379113












    • Thank you a lot!
      – AKarpun
      Feb 20 '16 at 20:26


















    • Thank you a lot!
      – AKarpun
      Feb 20 '16 at 20:26
















    Thank you a lot!
    – AKarpun
    Feb 20 '16 at 20:26




    Thank you a lot!
    – AKarpun
    Feb 20 '16 at 20:26












    up vote
    2
    down vote













    You have:



    str = "id/number/2000GXZ2/ref=sr"

    r = /
    (?:id/number/) # match string in a non-capture group
    ([a-zA-Z0-9]{8}) # match character in character class 8 times, in capture group 1
    /x # extended/free-spacing regex definition mode


    Then (using String#):



    str[r]
    #=> "id/number/2000GXZ2"


    returns the entire match, as it should, not just the contents of capture group 1. There are a few ways to remedy this. Consider first ones that do not use a capture group.



    @jacob.m suggested putting the first part in a positive lookbehind (modified slightly from his code):



    r = /
    (?<=id/number/) # match string in positive lookbehind
    [[:alnum:]]{8} # match >= 1 alphameric characters
    /x

    str[r]
    #=> "2000GXZ2"


    An alternative is:



    r = /
    id/number/ # match string
    K # forget everything matched so far
    [[:alnum:]]{8} # match 8 alphanumeric characters
    /x

    str[r]
    #=> "2000GXZ2"


    K is especially useful when the match to forget is variable-length, as (in Ruby) positive lookbehinds do not work with variable-length matches.



    With both of these approaches, if the part to be matched contains only numbers and capital letters, you may want to use [A-Z0-9]+ instead of [[:alnum:]]. In fact, if all the entries have the form of your example, you might be able to use:



    r = /
    d # match a digit
    [A-Z0-9]{7} # match >= 0 capital letters or digits
    /x

    str[r]
    #=> "2000GXZ2"


    If the substring to be matched is always followed by a non-alphanumeric character, you could replace {8} with +, {7} with *.



    The other line of approach is to keep your capture group. One simple way is:



    r = /
    id/number/ # match string
    ([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
    /x

    str =~ r
    $1 #=> "2000GXZ2"


    Alternatively, you could use String#sub to replace the entire string with the contents of the capture group:



    r = /
    id/number/ # match string
    ([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
    .* # match the remainder of the string
    /x

    str.sub(r, '1') #=> "2000GXZ2"
    str.sub(r, "\1") #=> "2000GXZ2"
    str.sub(r) { $1 } #=> "2000GXZ2"





    share|improve this answer























    • Wow, thank you for such thoroughful answer. Much apreciate it !
      – AKarpun
      Feb 20 '16 at 20:25















    up vote
    2
    down vote













    You have:



    str = "id/number/2000GXZ2/ref=sr"

    r = /
    (?:id/number/) # match string in a non-capture group
    ([a-zA-Z0-9]{8}) # match character in character class 8 times, in capture group 1
    /x # extended/free-spacing regex definition mode


    Then (using String#):



    str[r]
    #=> "id/number/2000GXZ2"


    returns the entire match, as it should, not just the contents of capture group 1. There are a few ways to remedy this. Consider first ones that do not use a capture group.



    @jacob.m suggested putting the first part in a positive lookbehind (modified slightly from his code):



    r = /
    (?<=id/number/) # match string in positive lookbehind
    [[:alnum:]]{8} # match >= 1 alphameric characters
    /x

    str[r]
    #=> "2000GXZ2"


    An alternative is:



    r = /
    id/number/ # match string
    K # forget everything matched so far
    [[:alnum:]]{8} # match 8 alphanumeric characters
    /x

    str[r]
    #=> "2000GXZ2"


    K is especially useful when the match to forget is variable-length, as (in Ruby) positive lookbehinds do not work with variable-length matches.



    With both of these approaches, if the part to be matched contains only numbers and capital letters, you may want to use [A-Z0-9]+ instead of [[:alnum:]]. In fact, if all the entries have the form of your example, you might be able to use:



    r = /
    d # match a digit
    [A-Z0-9]{7} # match >= 0 capital letters or digits
    /x

    str[r]
    #=> "2000GXZ2"


    If the substring to be matched is always followed by a non-alphanumeric character, you could replace {8} with +, {7} with *.



    The other line of approach is to keep your capture group. One simple way is:



    r = /
    id/number/ # match string
    ([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
    /x

    str =~ r
    $1 #=> "2000GXZ2"


    Alternatively, you could use String#sub to replace the entire string with the contents of the capture group:



    r = /
    id/number/ # match string
    ([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
    .* # match the remainder of the string
    /x

    str.sub(r, '1') #=> "2000GXZ2"
    str.sub(r, "\1") #=> "2000GXZ2"
    str.sub(r) { $1 } #=> "2000GXZ2"





    share|improve this answer























    • Wow, thank you for such thoroughful answer. Much apreciate it !
      – AKarpun
      Feb 20 '16 at 20:25













    up vote
    2
    down vote










    up vote
    2
    down vote









    You have:



    str = "id/number/2000GXZ2/ref=sr"

    r = /
    (?:id/number/) # match string in a non-capture group
    ([a-zA-Z0-9]{8}) # match character in character class 8 times, in capture group 1
    /x # extended/free-spacing regex definition mode


    Then (using String#):



    str[r]
    #=> "id/number/2000GXZ2"


    returns the entire match, as it should, not just the contents of capture group 1. There are a few ways to remedy this. Consider first ones that do not use a capture group.



    @jacob.m suggested putting the first part in a positive lookbehind (modified slightly from his code):



    r = /
    (?<=id/number/) # match string in positive lookbehind
    [[:alnum:]]{8} # match >= 1 alphameric characters
    /x

    str[r]
    #=> "2000GXZ2"


    An alternative is:



    r = /
    id/number/ # match string
    K # forget everything matched so far
    [[:alnum:]]{8} # match 8 alphanumeric characters
    /x

    str[r]
    #=> "2000GXZ2"


    K is especially useful when the match to forget is variable-length, as (in Ruby) positive lookbehinds do not work with variable-length matches.



    With both of these approaches, if the part to be matched contains only numbers and capital letters, you may want to use [A-Z0-9]+ instead of [[:alnum:]]. In fact, if all the entries have the form of your example, you might be able to use:



    r = /
    d # match a digit
    [A-Z0-9]{7} # match >= 0 capital letters or digits
    /x

    str[r]
    #=> "2000GXZ2"


    If the substring to be matched is always followed by a non-alphanumeric character, you could replace {8} with +, {7} with *.



    The other line of approach is to keep your capture group. One simple way is:



    r = /
    id/number/ # match string
    ([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
    /x

    str =~ r
    $1 #=> "2000GXZ2"


    Alternatively, you could use String#sub to replace the entire string with the contents of the capture group:



    r = /
    id/number/ # match string
    ([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
    .* # match the remainder of the string
    /x

    str.sub(r, '1') #=> "2000GXZ2"
    str.sub(r, "\1") #=> "2000GXZ2"
    str.sub(r) { $1 } #=> "2000GXZ2"





    share|improve this answer














    You have:



    str = "id/number/2000GXZ2/ref=sr"

    r = /
    (?:id/number/) # match string in a non-capture group
    ([a-zA-Z0-9]{8}) # match character in character class 8 times, in capture group 1
    /x # extended/free-spacing regex definition mode


    Then (using String#):



    str[r]
    #=> "id/number/2000GXZ2"


    returns the entire match, as it should, not just the contents of capture group 1. There are a few ways to remedy this. Consider first ones that do not use a capture group.



    @jacob.m suggested putting the first part in a positive lookbehind (modified slightly from his code):



    r = /
    (?<=id/number/) # match string in positive lookbehind
    [[:alnum:]]{8} # match >= 1 alphameric characters
    /x

    str[r]
    #=> "2000GXZ2"


    An alternative is:



    r = /
    id/number/ # match string
    K # forget everything matched so far
    [[:alnum:]]{8} # match 8 alphanumeric characters
    /x

    str[r]
    #=> "2000GXZ2"


    K is especially useful when the match to forget is variable-length, as (in Ruby) positive lookbehinds do not work with variable-length matches.



    With both of these approaches, if the part to be matched contains only numbers and capital letters, you may want to use [A-Z0-9]+ instead of [[:alnum:]]. In fact, if all the entries have the form of your example, you might be able to use:



    r = /
    d # match a digit
    [A-Z0-9]{7} # match >= 0 capital letters or digits
    /x

    str[r]
    #=> "2000GXZ2"


    If the substring to be matched is always followed by a non-alphanumeric character, you could replace {8} with +, {7} with *.



    The other line of approach is to keep your capture group. One simple way is:



    r = /
    id/number/ # match string
    ([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
    /x

    str =~ r
    $1 #=> "2000GXZ2"


    Alternatively, you could use String#sub to replace the entire string with the contents of the capture group:



    r = /
    id/number/ # match string
    ([[:alnum:]]{8}) # match >= 1 alphameric characters in capture group 1
    .* # match the remainder of the string
    /x

    str.sub(r, '1') #=> "2000GXZ2"
    str.sub(r, "\1") #=> "2000GXZ2"
    str.sub(r) { $1 } #=> "2000GXZ2"






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 17 '16 at 20:37

























    answered Feb 17 '16 at 20:29









    Cary Swoveland

    66.3k53865




    66.3k53865












    • Wow, thank you for such thoroughful answer. Much apreciate it !
      – AKarpun
      Feb 20 '16 at 20:25


















    • Wow, thank you for such thoroughful answer. Much apreciate it !
      – AKarpun
      Feb 20 '16 at 20:25
















    Wow, thank you for such thoroughful answer. Much apreciate it !
    – AKarpun
    Feb 20 '16 at 20:25




    Wow, thank you for such thoroughful answer. Much apreciate it !
    – AKarpun
    Feb 20 '16 at 20:25










    up vote
    0
    down vote













    This is Ruby Regexp expected match consistency evilness. Some Regexp-style methods will return the global-match while others will return specified matches.



    In this case, one method we can use to get the behavior you're looking for is scan.



    I don't think anyone here actually mentions how to get your Regexp working as you originally intended, which was to get the capture-only match. To do that, you would use the scan method like so with your original pattern:



    test_me.rb



    test_string="id/number/2000GXZ2/ref=sr"
    result = test_string.scan(/(?:id/number/)([a-zA-Z0-9]{8})/)
    puts result



    2000GXZ2



    That said, replacing (?:) with (?<=) for non-capture groups for look-behinds will benefit you both when you use scan as well as other parts of ruby that use Regexps.






    share|improve this answer

























      up vote
      0
      down vote













      This is Ruby Regexp expected match consistency evilness. Some Regexp-style methods will return the global-match while others will return specified matches.



      In this case, one method we can use to get the behavior you're looking for is scan.



      I don't think anyone here actually mentions how to get your Regexp working as you originally intended, which was to get the capture-only match. To do that, you would use the scan method like so with your original pattern:



      test_me.rb



      test_string="id/number/2000GXZ2/ref=sr"
      result = test_string.scan(/(?:id/number/)([a-zA-Z0-9]{8})/)
      puts result



      2000GXZ2



      That said, replacing (?:) with (?<=) for non-capture groups for look-behinds will benefit you both when you use scan as well as other parts of ruby that use Regexps.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        This is Ruby Regexp expected match consistency evilness. Some Regexp-style methods will return the global-match while others will return specified matches.



        In this case, one method we can use to get the behavior you're looking for is scan.



        I don't think anyone here actually mentions how to get your Regexp working as you originally intended, which was to get the capture-only match. To do that, you would use the scan method like so with your original pattern:



        test_me.rb



        test_string="id/number/2000GXZ2/ref=sr"
        result = test_string.scan(/(?:id/number/)([a-zA-Z0-9]{8})/)
        puts result



        2000GXZ2



        That said, replacing (?:) with (?<=) for non-capture groups for look-behinds will benefit you both when you use scan as well as other parts of ruby that use Regexps.






        share|improve this answer












        This is Ruby Regexp expected match consistency evilness. Some Regexp-style methods will return the global-match while others will return specified matches.



        In this case, one method we can use to get the behavior you're looking for is scan.



        I don't think anyone here actually mentions how to get your Regexp working as you originally intended, which was to get the capture-only match. To do that, you would use the scan method like so with your original pattern:



        test_me.rb



        test_string="id/number/2000GXZ2/ref=sr"
        result = test_string.scan(/(?:id/number/)([a-zA-Z0-9]{8})/)
        puts result



        2000GXZ2



        That said, replacing (?:) with (?<=) for non-capture groups for look-behinds will benefit you both when you use scan as well as other parts of ruby that use Regexps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 6 '17 at 0:56









        kayleeFrye_onDeck

        3,25022445




        3,25022445






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35462038%2fruby-regular-expression-non-capture-group%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

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules