Sign in to follow this  
maxest

[.net] Text searching with regular expressions

Recommended Posts

I have some sequence of chars, which looks like this:
abc*******abc*******************abc****...abc
So I have "words" which are separated by the "abc" string. How can I get these words? I've tried regex match like "abc*abc" but it doesn't work.

Share this post


Link to post
Share on other sites
The regular expression would be something like abc(.*?)abc - using . to match any character, * to match any number of them and ? to make the expression un-greedy.

Share this post


Link to post
Share on other sites
Ah, I see. Assume you had the following input:

abcHELLOabcWORLDabcGOODBYEabc

The first match here is abcHELLOabc, after which the cursor moves past the second abc. The next match after that is abcGOODBYEabc.

I suppose if you wanted to use this method (rather than the more sensible Split) you could do something like this:

Regex Expression = new Regex("abc(.*?)abc"); 
string Input = "abcHELLOabcWORLDabcTHISabcISabcFUNabc";

int StartPosition = 0;
for (; ; ) {
// Try and make a match.
var Match = Expression.Match(Input, StartPosition);
if (!Match.Success) break;
// We made a successful match!
Console.WriteLine(Match.Groups[1].Value);
// Move the start position along manually.
StartPosition = Match.Index + Match.Length - 3;
}


Alternatively you could do something like this:

Regex Expression = new Regex("abc(.*?)"); 
string Input = "abcHELLOabcWORLDabcTHISabcISabcFUNabc";
var Matches = Expression.Matches(Input);

...but you'd need to add some error checking to ensure that the original string ended in abc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this