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

  • Home
  • SEARCH
  • 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 8097209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:41:02+00:00 2026-06-05T21:41:02+00:00

I’m having trouble capturing the digits in a string of this format (t|b|bug_|task_|)1234 using

  • 0

I’m having trouble capturing the digits in a string of this format (t|b|bug_|task_|)1234 using a bash regex. The below doesn’t work:

[[ $current_branch =~ ^(t|b|bug_|task_|)([0-9]+) ]]

But once I change it to something like this:

[[ $current_branch =~ ^(t|b|bug_|task_)([0-9]+) ]]

it works, but of course its wrong because it doesn’t cover the case where there are no prefixes. I realize in this case I could do

[[ $current_branch =~ ^(t|b|bug_|task_)?([0-9]+) ]]

and achieve the same result, but I’d like to know why the 2nd example doesn’t work. That regex seems to work fine in Ruby, for example.

(This is on GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11), OSX Lion)

  • 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-05T21:41:04+00:00Added an answer on June 5, 2026 at 9:41 pm

    I’m certain that the difference between working and non-working versions of the regex are based in the different ways of reading regex (7). I’m going to quote the whole relevant part, because I think it goes to the heart of your issue:


    Regular expressions (“RE”s), as defined in POSIX.2, come in two forms: modern
    REs (roughly those of egrep; POSIX.2 calls these “extended” REs) and obsolete
    REs (roughly those of ed(1); POSIX.2 “basic” REs). Obsolete REs mostly exist
    for backward compatibility in some old programs; they will be discussed at the
    end. POSIX.2 leaves some aspects of RE syntax and semantics open; “(!)” marks
    decisions on these aspects that may not be fully portable to other POSIX.2
    implementations.

    A (modern) RE is one(!) or more nonempty(!) branches, separated by ‘|’. It
    matches anything that matches one of the branches.

    A branch is one(!) or more pieces, concatenated. It matches a match for the
    first, followed by a match for the second, etc.

    A piece is an atom possibly followed by a single(!) ‘*’, ‘+’, ‘?’, or bound.
    An atom followed by ‘*’ matches a sequence of 0 or more matches of the atom.
    An atom followed by ‘+’ matches a sequence of 1 or more matches of the atom.
    An atom followed by ‘?’ matches a sequence of 0 or 1 matches of the atom.

    A bound is ‘{‘ followed by an unsigned decimal integer, possibly followed by
    ‘,’ possibly followed by another unsigned decimal integer, always followed by
    ‘}’. The integers must lie between 0 and RE_DUP_MAX (255(!)) inclusive, and
    if there are two of them, the first may not exceed the second. An atom
    followed by a bound containing one integer i and no comma matches a sequence
    of exactly i matches of the atom. An atom followed by a bound containing one
    integer i and a comma matches a sequence of i or more matches of the atom. An
    atom followed by a bound containing two integers i and j matches a sequence of
    i through j (inclusive) matches of the atom.

    An atom is a regular expression enclosed in “()” (matching a match for the
    regular expression), an empty set of “()” (matching the null string)(!), a
    bracket expression (see below), ‘.’ (matching any single character), ‘^’
    (matching the null string at the beginning of a line), ‘$’ (matching the null
    string at the end of a line), a ‘\’ followed by one of the characters
    “^.[$()|*+?{\” (matching that character taken as an ordinary character), a ‘\’
    followed by any other character(!) (matching that character taken as an
    ordinary character, as if the ‘\’ had not been present(!)), or a single
    character with no other significance (matching that character). A ‘{‘
    followed by a character other than a digit is an ordinary character, not the
    beginning of a bound(!). It is illegal to end an RE with ‘\’.


    OK, there’s quite a lot here to unpack. First of all, note that the “(!)” symbol means that there is an open or non-portable issue.

    The essential issue is in the very next paragraph:

    A (modern) RE is one(!) or more nonempty(!) branches, separated by ‘|’.

    Your case is that you have an empty branch. As you can see from the “(!)”, the empty branch is an open or non-portable issue. I think this is why it works on some systems but not on others. (I tested it on Cygwin 4.1.10(4)-release, and it did not work, then on Linux 3.2.25(1)-release, and it did. The two systems have equivalent, but not identical, man pages for regex7.)

    Assuming that branches must be nonempty, a branch can be a piece, which can be an atom.

    An atom can be “an empty set of “()” (matching the null string)(!)”. <sarcasm>Well, that’s really helpful.</sarcasm> So, POSIX specifies a regular expression for the empty string, i.e. (), but also appends a “(!)”, to say that this is an open issue, or not portable.

    Since what you’re looking for is a branch that matches the empty string, try

    [[ $current_branch =~ ^(t|b|bug_|task_|())([0-9]+) ]]
    

    which uses the () regex to match the empty string. (This worked for me in my Cygwin 4.1.10(4)-release shell, where your original regex didn’t.)

    However, while (hopefully) this suggestion will work for you in your current setup, there’s no guarantee that it will be portable. Sorry to disappoint.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.