473,729 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex equivalent of Substring

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.Subs tring(iIndexPos ition, 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.


Nov 16 '05 #1
3 10308
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.

--
<%= Clinton Gallagher
METROmilwaukee "Regional Information Services"
NET csgallagher AT metromilwaukee. com
URL http://clintongallagher.metromilwaukee.com/

"Tim Soliday" <ti********@yah oo.com> wrote in message
news:Xn******** *************** ***********@207 .46.248.16...
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.Subs tring(iIndexPos ition, 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.

Nov 16 '05 #2
Tim Soliday <ti********@yah oo.com> wrote in
news:Xn******** *************** ***********@207 .46.248.16:
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.Subs tring(iIndexPos ition,
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("AA BBCC", "BB");

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

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

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #3
> 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("AA BBCC", "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.
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1972
by: mikea59 | last post by:
I am getting errors in XMLSpy (Pro) in the following case: Source Document: <test> 12345 AB 12345 </test> Stylesheet: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
7
5727
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning is to improve, but not to prove.
5
3063
by: Tap | last post by:
I would like to parse the following string with as little code as possible. stringValue = "Email Message ID:TAPASVI to tapsbin@hotmail.com;tapasbin@cs.com;{CC}tapstemp@hotmail.com;{CC}tapstemp007@hotmail.com" 1) Take everything in between "ID:" and the word " to" i.e. stringVal1 = "TAPASVI" 2) Take between the word " to " and "{CC}" i.e. stringVal2 = "tapsbin@hotmail.com;tapasbin@cs.com;"
4
3851
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
17
1653
by: steve | last post by:
here's the deal...cvs, tick encapsulted data. trying to use regex's to validate records. here's an example row: 'AD,'BF','132465','06/09/2004','','BNSF','A','TYPE','1278','','BR','2999','' ,'LX','','01','09','1','','','','','','','','','CUSTOM JOB CODE TEST' record type is in the 8th column ('1278'). using regex b/c there are a miriad of types that cause other data w/n the record (or related records) to be in/valid. i'm having problems...
4
4543
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
16
2251
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string) pattern search functions. Where performance is an issue you can alway write your own specialized routine of course. However, for the occasional pattern search where performance isn't an issue, would most seasoned .NET developers rely on "Regex" and...
4
1948
by: Flomo Togba Kwele | last post by:
I am having difficulty writing a Regex constructor. A line has a quote(") at its beginning and its end. I need to strip both characters off. If the line looks like "1", I need the result to be 1. If it were sed, I could just do s/^"// and s/"$//, but I'm confused with the quote escape characters. Also, can I do both replacements at once? I tried:
3
2590
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Two part question: 1. Is Regex more efficient than manually comparing values using Substring? 2. I've never created a Regex expression. How would I use regex to do the equivalent of what I have coded using Substrings below? string s = TextBox1.Text.ToUpper(); string ch = s.Substring(0, 1); // read first character if ((ch == "B") || (ch == "C") || (ch == "X")) {
0
8764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9156
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8159
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6026
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4798
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2694
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.