Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9141073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:35:55+00:00 2026-06-17T09:35:55+00:00

I am using Ruby1.9.3. I am newbie to this platform. From the doc I

  • 0

I am using Ruby1.9.3. I am newbie to this platform.

From the doc I just got familiared with two anchor which are \z and \G. Now I little bit played with \z to see how it works, as the definition(End or End of String) made me confused, I can’t understand what it meant say – by End. So I tried the below small snippets. But still unable to catch.

CODE

irb(main):011:0> str = "Hit him on the head me 2\n" + "Hit him on the head wit>
=> "Hit him on the head me 2\nHit him on the head with a 24\n"
irb(main):012:0> str =~ /\d\z/
=> nil

irb(main):013:0> str = "Hit him on the head me 24 2\n" + "Hit him on the head >
=> "Hit him on the head me 24 2\nHit him on the head with a 24\n"
irb(main):014:0> str =~ /\d\z/
=> nil

irb(main):018:0> str = "Hit1 him on the head me 24 2\n" + "Hit him on the head>
=> "Hit1 him on the head me 24 2\nHit him on the head with a11 11 24\n"
irb(main):019:0> str =~ /\d\z/
=> nil
irb(main):020:0>

Every time I got nil as the output. So how the calculation is going on for \z ? what does End mean? – I think my concept took anything wrong with the End word in the doc. So anyone could help me out to understand the reason what is happening with the out why so happening?

And also i didn’t find any example for the anchor \G . Any example please from you people to make visualize how \G used in real time programming?

EDIT

irb(main):029:0>
irb(main):030:0*  ("{123}{45}{6789}").scan(/\G(?!^)\{\d+\}/)
=> []
irb(main):031:0>  ('{123}{45}{6789}').scan(/\G(?!^)\{\d+\}/)
=> []
irb(main):032:0>

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:35:56+00:00Added an answer on June 17, 2026 at 9:35 am

    \z matches the end of the input. You are trying to find a match where 4 occurs at the end of the input. Problem is, there is a newline at the end of the input, so you don’t find a match. \Z matches either the end of the input or a newline at the end of the input.

    So:

    /\d\z/
    

    matches the “4” in:

    "24"
    

    and:

    /\d\Z/
    

    matches the “4” in the above example and the “4” in:

    "24\n"
    

    Check out this question for example of using \G:
    Examples of regex matcher \G (The end of the previous match) in Java would be nice


    UPDATE: Real-World uses for \G

    I came up with a more real world example. Say you have a list of words that are separated by arbitrary characters that cannot be well predicted (or there’s too many possibilities to list). You’d like to match these words where each word is its own match up until a particular word, after which you don’t want to match any more words. For example:

    foo,bar.baz:buz’fuzz*hoo-har/haz|fil^bil!bak

    You want to match each word until ‘har’. You don’t want to match ‘har’ or any of the words that follow. You can do this relatively easily using the following pattern:

    /(?<=^|\G\W)\w+\b(?<!har)/
    

    rubular

    The first attempt will match the beginning of the input followed by zero non-word character followed by 3 word characters (‘foo’) followed by a word boundary. Finally, a negative lookbehind assures that the word which has just been matched is not ‘har’.

    On the second attempt, matching picks back up at the end of the last match. 1 non-word character is matched (‘,’ – though it is not captured due to the lookbehind, which is a zero-width assertion), followed by 3 characters (‘bar’).

    This continues until ‘har’ is matched, at which point the negative lookbehind is triggered and the match fails. Because all matches are supposed to be “attached” to the last successful match, no additional words will be matched.

    The result is:

    foo
    bar
    baz
    buz
    fuzz
    hoo
    

    If you want to reverse it and have all words after ‘har’ (but, again, not including ‘har’), you can use an expression like this:

    /(?!^)(?<=har\W|\G\W)\w+\b/
    

    rubular

    This will match either a word which is immediately preceeded by ‘har’ or the end of the last match (except we have to make sure not to match the beginning of the input). The list of matches is:

    haz
    fil
    bil
    bak
    

    If you do want to match ‘har’ and all following words, you could use this:

    /\bhar\b|(?!^)(?<=\G\W)\w+\b/
    

    rubular

    This produces the following matches:

    har
    haz
    fil
    bil
    bak
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie to ruby this is my first project I am using the FasterCSV Gem
Apologies -- I'm a newbie using Ruby on Rails. Still a little confused about
I am newbie in Ruby. I installed by following this tutorial: http://udooz.net/blog/2011/02/facebook-app-using-rails-koala/ Now, when
I'm definitely a newbie to ruby (and using 1.9.1), so any help is appreciated.
Using Ruby, I'm trying to parse some documentation in which I need to split
Using Ruby (newb) and Regex, I'm trying to parse the street number from the
I am a newbie with ruby and was trying out something small. So this
I'm a newbie at ruby. While I am using irb, something happens.(Nothings are printed)
I was trying to install rails on Ubuntu Natty Narwhal 11.04, using ruby1.9.1. I
I'm an EM newbie and writing two codes to compare synchronous and asynchronous IO.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.