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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:04:55+00:00 2026-06-03T05:04:55+00:00

I am using the regexec() function in C. I basically am trying to write

  • 0

I am using the regexec() function in C. I basically am trying to write a regular expression to capture portions of a string for substitution.

So for example, if I have the string “Hello $X” Then I want the regexec to give me the range 6,7 as that is “$X”. But as there can be an arbitrary number of substitutions, I am using the regular expression:

"([^$]*(\\$[A-Za-z][A-Za-z0-9_]*))+"

This should match any arbitrary sequence of text + substitution patterns.

So for example in the string “First=$X, Second=$Y” I need to know that $X occurred at offset 6-7 and and $Y occurred at offset 17-18.

The actual offsets I get from regexec are:
0,19 8,19 17,19

First, I understand that the ending offset is actually one past the the character of the match. So the above offsets correspond to the following parts of the string:

First=$X, Second=$Y
, Second=$Y
$Y

Now I can see what is happening here: the first range is obviously the entire match, and the second is the first entire sub-match of the second sub-expression. But from this point on I am puzzled. Why is it only returning the first sub-match of the second sub-expression and not the first?

I suspect it has something to do with the fact that I have a repeating expression, but I’m not sure what I need to do to fix the problem. How do I get it to return the desired offsets?

Note: I am passing a 128-element regmatch_t to regexec() (nmatch=128), so I should be able to get all matches.

  • 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-03T05:04:56+00:00Added an answer on June 3, 2026 at 5:04 am

    You’re confused about what first and second mean. In this expression:

    "([^$]*(\\$[A-Za-z][A-Za-z0-9_]*))+"
     ^_______________________________^    this part
    

    is the first parenthesizes subexpression and

    "([^$]*(\\$[A-Za-z][A-Za-z0-9_]*))+"
           ^________________________^    this part
    

    is the second. If a parenthesized subexpression gets used more than once as part of a *, ?, +, or {} repetition operator, it’s the last match that counts.

    If you want to match an arbitrary number of instances, than rather than using the + on the end of your regex, you simply need to call regexec multiple times, and use the ending offset of the previous run as your new starting point.

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

Sidebar

Related Questions

I am writing a function to find variables in a string using regular expression.
I'm trying to match a string 123,1234 using regex.h. Following pattern does the job:
I am trying to match a pipe character in a string using a Python
I'm trying to write a function in python, which will determine what type of
Using Nunit, I want to be able to write a test fixture that will
Using C# for ASP.NET and MOSS development, we often have to embed JavaScript into
Using the javascript function function squareIt(number) { return number * number; } When given
using http://www.regular-expressions.info/javascriptexample.html I tested the following regex ^\\{1}([0-9])+ this is designed to match a
I'm writing a regular expression to match data from the IMDb soundtracks data file.
On my OS X 10.5.8 machine, using the regcomp and regexec C functions to

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.