Regex equivalent of Substring

T

Tim Soliday

I have been looking at regular expressions lately, and I'm hoping that
maybe someone could help me?

I am trying to find the position of a substring, and then returning the
substring including any information that I might want whether it is to the
left or right of that substring.

For example:

//Position of substring has already been determined
string sReturnData = sParseData.Substring(iIndexPosition, iLength);

Is there a way of doing this using regular expressions?

The code above is the equivalent to Visual Basic's Left, I think. (I do not
use VB so this would be a guess for me)

The reason why I want to use regular expressions is because my application
uses a c# scripting engine. It puts out a script file, and I want to allow
the user to change the script if they want to.
 
C

clintonG

I can recommend a very inexpensive book that despite the title is the best
work in this context I have ever read, "Teach Yourself Regular Expressions
in 10 Minutes" written by Ben Forta who is a well known Macromedia web
developer.
 
C

Chris R. Timmons

I have been looking at regular expressions lately, and I'm
hoping that maybe someone could help me?

I am trying to find the position of a substring, and then
returning the substring including any information that I might
want whether it is to the left or right of that substring.

For example:

//Position of substring has already been determined
string sReturnData = sParseData.Substring(iIndexPosition,
iLength);

Is there a way of doing this using regular expressions?

The code above is the equivalent to Visual Basic's Left, I
think. (I do not use VB so this would be a guess for me)

The reason why I want to use regular expressions is because my
application uses a c# scripting engine. It puts out a script
file, and I want to allow the user to change the script if they
want to.

Tim,

The Match class provides all of that information. It has Index and
Length properties for each match. It also has a Result method you
can use to get the text to the right and left of the match:

Match myMatch = Regex.Match("AABBCC", "BB");

// myMatch.Index = 2, myMatch.Length = 2.

string leftText = myMatch.Result("$`"); // AA
string rightText = myMatch.Result("$'"); // CC
 
T

Tim Soliday

Tim,
The Match class provides all of that information. It has Index and
Length properties for each match. It also has a Result method you
can use to get the text to the right and left of the match:

Match myMatch = Regex.Match("AABBCC", "BB");

// myMatch.Index = 2, myMatch.Length = 2.

string leftText = myMatch.Result("$`"); // AA
string rightText = myMatch.Result("$'"); // CC

Thank you for the help. That is exactly what I am looking for. I tried it
out and it worked perfectly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top