473,626 Members | 3,936 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 10305
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
1968
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
5720
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
3060
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
3838
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
1645
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
4540
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
2244
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
1942
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
2582
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
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8638
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8505
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
7196
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
6125
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
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.