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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:20:01+00:00 2026-06-16T05:20:01+00:00

Consider such code: Match match = new Regex(^bar).Match(some_string,3); I would like to match some_string

  • 0

Consider such code:

Match match = new Regex("^bar").Match(some_string,3);

I would like to match some_string but not from the beginning of it, but starting from given position. The catch is, I would like to make a match exactly from that position, thus the anchor.

Unfortunately, it does not work. MS regex does not work with chunks of the string as strings on their own, so ^ matches only the one and true beginning of the string not of starting position.

The easy workaround is to write this in such way:

Match match = new Regex("^bar").Match(some_string.Substring(3));

The downsize — speed (i.e. lack of it).

So my question is this — how to match anchored regex in the middle of the string, fast?

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

    .NET has a separate anchor for the position where the engine starts (usually the end of the last match, in your case the offset): \G.

    So you should be able to use the pattern @"\Gbar".

    Source (MSDN)

    If you want to restrict a match so that it begins at a particular character position in the string and the regular expression engine does not scan the remainder of the string for a match, anchor the regular expression with a \G (at the left for a left-to-right pattern, or at the right for a right-to-left pattern). This restricts the match so it must start exactly at startat.

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

Sidebar

Related Questions

I would like to compile fortran code on mac such that it does not
Consider such code (this is just example not real code): class Foo(url : String)
How can I unproxy a hibernate object, such that polymorphism would be supported? Consider
Consider the following code (from here , with the number of tests increased): from
I have an unusual problem. Let's consider such models (taken from django docs): class
I'm not really into type-safety as a concept but many consider it important for
Please, consider the following (I'm sorry for the amount of code; but this is
Please consider such scenerio: I have component called TMenuItemSelector which has two published properties:
Background Consider the validation of three doubles low , width and high such that
In Algorithm Design Manual , there is such an excise 4-26 Consider the problem

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.