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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:36:06+00:00 2026-06-15T02:36:06+00:00

I experience a strange behaviour when working with RegEx. dataString = #Name #Location New

  • 0

I experience a strange behaviour when working with RegEx.

dataString = "#Name #Location New York #Rating"
string[] rawValues = Regex.Split(dataString.Trim(), "(^|\\s)+#\\w+");

The pattern matches: "#Name", " #Location", " #Rating" (which is what I intend to match).
The split returns: ["", "", "", " ", "New York", " ", ""]

Question #1: The cunfusion starts already here. Why are there empty strings at positions 0,1,2? Two for the matches and one because it was at the first position of the string?

But this was not the strange part.

string[] rawValues = Regex.Split(dataString.Trim(), "(\\s|^)+#(\\w*[A-Za-z_]+\\w*)");

The pattern matches: "#Name", " #Location", " #Rating" (the same as before).
But the split returns: ["", "", "Name", "", " ", "Location"," New York", " ", "Rating",""]

Question #2: A pattern which leads to the exact same match, results in a totally different split output. How is this possible??

  • 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-15T02:36:07+00:00Added an answer on June 15, 2026 at 2:36 am

    The reason is this sentence from MSDN:

    If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array.

    You you shouldn’t use capturing groups in Split if you really just want to split the string at matches. You can avoid capturing groups, by using (?:...) in place of every (...) you have.

    Plus, as you correctly assumed. The first and last "" originate from the fact that the string starts and ends with a match (so the empty string before and after these matches will be reported in the split).

    Here is a regular expression that is better suited for you purposes:

    @"(?:^|\s+)#\w*[A-Za-z_]+\w*"
    

    Note that having the + outside of your first subpattern was also unnecessary and led to awkward side effects. Firstly it allowed the group to capture multiple times (which is why you got two addition "", "": one for ^ and one for \s). Secondly, there is no need to repeat ^ after the first space character has been matched, so it is enough to repeat only the space character. Also, there is no need to group the word after # at all.

    However, if all you want is to match something like #name when it is at the start of the string or preceded by a space (i.e. not* preceded by a **non-space character), why include possible spaces in the match at all. A negative lookbehind gives you a nice way out:

    @"(?<!\S)#\w*[A-Za-z_]+\w*"
    

    This does exactly what described above. The (?<!\S) matches if there is no non-space character left to it (without including a space-character in the match if there is one). That covers both cases without alternation, and you don’t need to Trim your key names.

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

Sidebar

Related Questions

I'm using a SortedDictonary(Of String, String) in my application, and I experience a strange
I experience a strange behaviour in Visual Studio 2010 when creating Windows Phone 7
Recently, I started to experience strange behaviour in JBOss 4.0.5 JSP application. The JSP
I experience strange problem. We have error handling in global.asax that would redirect user
I experience a very strange problem and I don't have found any help to
I experienced very strange behaviour with a CakePHP site in production. Some views did
I've encountered some very strange behaviour when debugging in VS 2010 with a WCF
On my website, I experience a strange IE8 bug, where IE8 crashes with message:
I'm working locally on an ASP.NET website under Windows 7, and I experience stange
I'm facing strange OOP behaviour (IMO) in android and maybe in Java in general...

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.