Link to home
Start Free TrialLog in
Avatar of roricka
roricka

asked on

Is it true C#.NET has no "global flag" for regular expressions?

Many regular expression systems, such as the one for JavaScript, require some kind of "global flag" for a pattern to be matched everywhere  (e.g., the regular expression /xyz/g means match "xyz" throughout the target string). Otherwise the match is made only once. Thus for a replacement, all occurrences of the match are replaced (the "g" case) or only the first one (the default case).

C#.NET doesn't seem to have a "global flag". Is this correct -- the default behavior is match every occurrence? And if so, how would you specify to match (e.g., replace) only the first occurrence?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of roricka
roricka

ASKER

You pointed me in the right direction. I should have asked how to have Replace only replace the first occurrence. I see that Replace is overloaded. By default it replaces all matches, but it is possible to specify the number of replacements (including just 1).

Javascript's approach is clearly less sophisticated, but it was confusing, since in that system the global specifier is given with the regular expression, not implied as an argument to "replace" (note: lower-case "r" for Javascript.)

Anyway, thanks. I marked the solution was "partially" complete more because of my poorly worded question than any fault on your part.
Not a problem, glad I was able to get you pointed in the right direction. Have a great day  ;=)