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 9146681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:48:22+00:00 2026-06-17T10:48:22+00:00

I was trying to understand how .{n} and ?<option>: works in Regexp on Ruby

  • 0

I was trying to understand how .{n} and ?<option>: works in Regexp on Ruby 1.9.3 environment. But couldn’t understand how the below code produce the output:

irb(main):001:0> %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.{2}(?m:.)\Z/
=> ["fin\n", "fin\r\n", "find"]
irb(main):002:0> %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.{1}(?m:.)\Z/
=> ["fin\n", "fi\n\n"]
irb(main):003:0> %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.{1}(?m:.)\Z/
=> []
irb(main):010:0> %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.(?m:.)\Z/
=> ["fin\n", "fi\n\n"]
irb(main):011:0> %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.(m:.)\Z/
=> []
irb(main):012:0> %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.(?m:.)\z/
=> []

Can anyone help me to understand how the above code worked to generate the mentioned output in IRB terminal?

Thanks,


As per @Kevin last paragraph I tried below and found expected and desirable output :

irb(main):014:0> %W{fin fi\n\n \n\n fin\r\n find}.grep /f.(?m:.)\z/
=> ["fin"]
irb(main):015:0> %W{fin fi\n\n \n\n fin\r find}.grep /f.(?m:.)\z/
=> ["fin"]
irb(main):016:0> %W{fin fi\n \n\n fin\r\n find}.grep /f.(?m:.)\z/
=> ["fin", "fi\n"]
irb(main):017:0> %W{fin fi\n \n\n fr\n find}.grep /f.(?m:.)\z/
=> ["fin", "fi\n", "fr\n"]
irb(main):018:0>

Thank you very much @Kevin . You helped me to understand the whole concept!

  • 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-17T10:48:23+00:00Added an answer on June 17, 2026 at 10:48 am

    {n} means “repeat the previous atom n times”. In regular expressions, an atom is a self-contained unit. So a single character is an atom. So is a dot. A group is an atom as well (that contains other atoms), as is a character class. So .{n} means “match n characters” (because . means “match any character”).

    Note that {n} is not like a backreference, in that it doesn’t have to match the same text on each repetition. .{5} behaves exactly like ......

    This construct is also more powerful. It can take two numbers, and it matches a repetition count for that whole range. So .{3,5} means “match 3 to 5 characters”. And .{3,} means “match 3 or more characters”. ? can be replaced with {0,1}, * with {0,}, and + with {1,} if you so desired.


    ?<option: isn’t actually a thing. It’s (?<option>:<pattern>), and this turns on all the flags listed in <option> for the duration of <pattern>. It’s like a group, except it doesn’t actually create a back reference. So the expression (?m:.) means “match one character as if the flag m was turned on”. Given the behavior of m as “match \n” as nhahtdh said in the comments, the expression .(?m:.). means “match any character besides newline, followed by any character, followed by any character besides newline”.

    This construct has two benefits. First, it allows you to only have a flag apply to part of a pattern, which can be occasionally useful. And second, if you wrap your entire pattern in this construct, then you have control over the flags that apply to your regular expression regardless of where the expression is used. This is useful when you’re providing the regex as a user and don’t have control over the source of the program.


    Let’s take a look at the examples you gave:

    > %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.{2}(?m:.)\Z/
    => ["fin\n", "fin\r\n", "find"]
    

    Your pattern /f.{2}(?m:.)\Z/ means “match f, followed by 2 of any character (but newline), followed by any character, and anchor to the end of the string or just before a newline”.

    So in each of the 3 matches, fin matches the f.{2}. (?m:.) matches \n in the first, \r in the second, and d in the third. And \Z matches the end of the string in the first, just before a newline in the second, and the end of the string in the third.

    fi\n\n doesn’t match because the first \n here can’t be matched by the . from .{2} without the m flag.

    > %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.{1}(?m:.)\Z/
    => ["fin\n", "fi\n\n"]
    

    Here fi matches f.{1} in both cases. (?m:.) matches n and \n, and \Z matches before the newline in both cases.

    fin\r\n doesn’t match because \Z will only match before the final newline in the string, not before a CRLF pair. And find doesn’t match because there’s nothing to match the d.

    > %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.{1}(?m:.)\Z/
    => []
    

    I think you have a copy & paste error here. This is identical to the previous pattern and matches as that does.

    > %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.(?m:.)\Z/
    => ["fin\n", "fi\n\n"]
    

    This is also identical to the previous pattern. . and .{1} are the same thing. In fact, {1} can always be stripped from any regular expression without changing anything.

    > %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.(m:.)\Z/
    => []
    

    You dropped the ? in this pattern, changing the meaning of (m:.). This no longer changes options. Now it’s just a capturing group that matches the pattern m:., which of course doesn’t occur in your input.

    > %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.(?m:.)\z/
    => []
    

    You changed \Z to \z here. The difference between those two is \Z may match before a trailing newline, but \z must only match the end of the string. Without being able to match before the trailing newline, none of your inputs here match. But, for example, if you had fin (without the newline) or fi\n (without the second newline) it would work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
I am trying to scrap some content from a website but the code below
I am trying to understand how this piece of self-replicating code works (found here
I'm trying to understand how Backbone.js model validation works, but I'm seeing some odd
So, I inherited some code (below) from someone else and I'm trying to understand
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand the math of this code snippet. A token is provided which
I have written the following code which works, but im wondering can I make
I'm trying to understand how Slick works and how to use it... and looking
I am trying to understand how native code can interact with .NET code and

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.