PC Review


Reply
Thread Tools Rate Thread

I need an workaround for Regex limitation

 
 
Liviu Uba
Guest
Posts: n/a
 
      14th Apr 2006
Hi,

I noticed a strange behaviour, strange from my point of view anyway.

Let's say I have the regex expression: "^\w+".
When I do:

Let's say s = "first second";

Regex r = new Regex( "^\w+", RegexOptions.CaseInsensitive...);
Match m = r.Match(s) ; // here we have m.Succes = true;

m = r.Match(s,6); //here we have m.Success = false;

I would have expected that the ^ will match the beginning of the actual
text queried, but that is not the case.
The fact that ^ does not match this type of call, is possible to work around
but it means a serious performance problem: to match the expression without
^ and to verify that the first match starts from 0, or to copy the string
from the position I query which is not an option for a huge string.

Has anyone got a clue how to make ^ working?




 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      14th Apr 2006
The '^' operator works just fine. It matches only if the word character
sequence starts at the beginning of the string. Using Regex.Match(s, 6) you
get all Matches that exist beyond index 6 in the string. Since there are
none, you get none back. You think that it should count the index as the
beginning of the string because you are only thinking about your specific
problem. The index is *not* the beginning of the string. Consider the
following, for example:

string[] strings = new string[] {"one", "two", "three", "four", "five",
"six"};
string s = "one twothree four fivesix";
string newResult;
Regex r = new Regex("\\w");

for (int i = 0; i < strings.Length; i++)
{
newResult = r.Match(s, s.IndexOf(strings[i]));
}

In this case, you are looking for any match in the string that is found in
the array, and your results depend upon the position in the string. If the
Match returned is null, the item in the array is not in the string. The
string "one" would be found, as would the string "four." But the strings
"twothree" and "fivesix" would not be found. Now, if you were to use "\w+"
you would not be able to find any other Match than the first.

In other words, the beginning of the string is the logical beginning of the
string. The index in the string is not relevant or related to the beginning
of the string.

Now, if you can state the business rule you're trying to satisfy, I think I
can help with a solution.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Liviu Uba" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I noticed a strange behaviour, strange from my point of view anyway.
>
> Let's say I have the regex expression: "^\w+".
> When I do:
>
> Let's say s = "first second";
>
> Regex r = new Regex( "^\w+", RegexOptions.CaseInsensitive...);
> Match m = r.Match(s) ; // here we have m.Succes = true;
>
> m = r.Match(s,6); //here we have m.Success = false;
>
> I would have expected that the ^ will match the beginning of the actual
> text queried, but that is not the case.
> The fact that ^ does not match this type of call, is possible to work
> around but it means a serious performance problem: to match the expression
> without ^ and to verify that the first match starts from 0, or to copy the
> string from the position I query which is not an option for a huge string.
>
> Has anyone got a clue how to make ^ working?
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with string length limitation workaround kristopher.dawsey@gmail.com Microsoft Excel Programming 3 13th Feb 2007 12:49 AM
Workaround for WaitHandle limitation? =?Utf-8?B?RGF2ZSBCb29rZXI=?= Microsoft C# .NET 14 6th Nov 2006 01:42 PM
Formula Req'd - Autofilter limitation workaround =?Utf-8?B?TWF1cmljZQ==?= Microsoft Excel Worksheet Functions 4 12th Sep 2006 11:06 PM
Need Workaround for Cell Display Limitation in Excel 2000 =?Utf-8?B?S2V2aW4=?= Microsoft Excel Misc 5 20th Apr 2005 11:33 PM
Any workaround for rules limitation?? =?Utf-8?B?RWQgQ2F5Y2U=?= Microsoft Outlook 1 6th Oct 2004 10:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:52 PM.