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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:32:14+00:00 2026-05-14T05:32:14+00:00

This is the part of a string 21xy5. I want to insert * surrounded

  • 0

This is the part of a string “21xy5″. I want to insert ” * ” surrounded with whitespace between: digit and letter, letter and digit, letter and letter. I use this regex pattern “\d[a-z]|[a-z]\d|[a-z][a-z]” to find indexs where I gona insert string ” * “. Problem is that when regex OR(|) in string 21xy5 trays to match 21-x|x-y|y-5, when first condition 21-x success, second x-y is not checked, and third success. So I have 21 * xy * 5 instead 21 * x * y * 5. If input string is like this xy21, then x-y success and then I have x * y21. Problem is that logical OR is not greedy.

    Regex reg = new Regex(@"\d[a-z]|[a-z]\d|[a-z][a-z]" );
    MatchCollection matchC;
    matchC = reg.Matches(input);
    int ii = 1;
    foreach (Match element in matchC)
    {
        input = input.Insert(element.Index + ii, " * ");
        ii += 3;
    }
    return input;
  • 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-14T05:32:15+00:00Added an answer on May 14, 2026 at 5:32 am

    You want lookarounds.

    Regex reg = new Regex(@"(\d(?=[a-z])|[a-z](?=[a-z\d]))");
    

    (Replace reg with $1 *)

    The problem of your original regex is not greediness, but it will actually consume 2 characters. That means, when 1x is being matched, only y5 will be left available, so the regex engine cannot see the xy. OTOH, look-ahead is just a zero-width assertion, so the next character will not be consumed. e.g. while 1x together matches \d(?=[a-z]), only 1 will be consumed, so xy5 is available.

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

Sidebar

Related Questions

I want to split a string like this: abc//def//ghi into a part before and
I want to get this bold part from this string: some other code src='/pages/captcha?t=c&s=**51afb384edfc&h=513cc6f5349b**'
How to cut part from this string... abb.c.d+de.ee+f.xxx+qaa.+.,,s, ... where i know position by
This question is extended part of my previous question, Finding number position in string
This question is a combination of regex practice and unit testing practice. Regex part
I have this part of code string query = SELECT ID, COL1 FROM TABLE1
I have string like this: http://someurl.com?test&lettersg and I would like to mach part from
I'm trying to explode this part of a json string from Google's currency calc:
Using powershell I want to replace a specific numeric part of a string with
Note To Self; This part of the string should not be changed because it

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.