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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:09:30+00:00 2026-05-28T05:09:30+00:00

[regex]::replace(‘test test’,’^(.*?)test’, ‘barf’) returns ‘barf test’ Why doesn’t it replace all occurrences of ‘test’?

  • 0
[regex]::replace('test test','^(.*?)test', 'barf')

returns ‘barf test’

Why doesn’t it replace all occurrences of ‘test’? This must have something to do with the position at which a subsequent replace iteration begins.

  • 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-05-28T05:09:30+00:00Added an answer on May 28, 2026 at 5:09 am

    Quick answer: you anchored it at the beginning of the input (^) and your first group ((.*?)) did not capture anything (since the first occurrence of test was found right after the beginning of line and you use a lazy quantifier — furthermore you don’t use the capture in your replacement string. Had you used a “normal” quantifier, the last occurrence of test would have been replaced).

    Long answer: a regex never needs to match the whole input, only the parts which are necessary. What’s more, when cycling through an input, the regex engine will start the next round from the position where it successfully completed a match.

    Here, you want to replace a sequence of characters which is test. Note that it will also means that testosterone will be matched (or untested). If you want to match test as a “word”, use the word anchor \b.

    This works (tested on Powershell v2):

    [regex]::replace('test test','\btest\b', 'barf')
    

    The engine in action looks something like this:

    # beginning
    regex: |\btest\b
    input: |test test
    # \b: matched,  beginning of input followed by word character
    regex: \b|test\b
    input: |test test
    # literal matching of t, then e, then s, then t
    regex: \btest|\b
    input: test| test
    # \b: match, word character followed by non word character
    regex: \btest\b|
    input: test| test
    # replacement
    regex: \btest\b|
    input: barf| test
    # beginning of second round
    regex: |\btest\b
    input: barf| test
    # \b: match, word character followed by non word character
    regex: \b|test\b
    input: barf| test
    # t: not matched. Failed matching. Proceeding to next character
    regex: |\btest\b
    input: barf |test
    # \b: match
    regex: \b|test\b
    input: barf |test
    # literal matching of t, then e, then s, then t
    regex: \btest|\b
    input: barf test|
    # \b: match, word character followed by end of input
    regex: \btest\b|
    input: barf test|
    # replacement
    regex: \btest\b|
    input: barf barf|
    # beginning of next round
    regex: |\btest\b
    input: barf barf|
    # end of input: end of processing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems the .NET Regex.Replace method automatically replaces all matching occurrences. I could provide
I have this statement: String cap = Regex.Replace(winCaption, @[^\w\.@-], ); that transforms Hello |
I have this simple regex replace based routine, is there anyway to improve its
The obvious attempt is: Regex.Replace(input, @.$, X, RegexOptions.Singleline); This doesn't always work though. Consider
Assume I have this: Regex.Replace(aa cc bbbb,aa cc,,RegexOptions.IgnoreCase); But I also need to ignore
why this regex not work? i want to replace my string by all not
I am using c# regex.replace and I want to replace this string: test =
I have the following regex: foo = Regex.Replace(foo, @[^a-zA-Z0-9\s-], ); Currently, this removes Unicode
Possible Duplicate: regex replace all ignore case I need to replace all occurrences of
I have this: Title = Regex.Replace(Title, s, <span style=\background:yellow\> + s + </span>, RegexOptions.IgnoreCase);

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.