473,508 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting a portion of a string

I have some text where I need to extract some pieces from. The text will be
in a format like this:

a string description color="red" type="unknown"

In the above example, I would be looking to extract the word "red".

There's couple of ways I could approach the problem. I could use IndexOf to
search for the string 'color=' and then extract the value using the Substr
method. Or, I could use a regular expression like:
color="([a-z]|[A-Z])+"

In this case, the Match method would return the string 'color="red"'. I
could then use the Substr method to extract the string 'red'. Of course,
using a regular expression might be a little overkill in this case.

I'm just wondering if there is, buried in the 1500+ classes of .NET, a
better approach. Or, if there is a way of getting a regular expression to
return just the string within the quotes.

Richard
Jul 21 '05 #1
3 1600
The following C# code should do the trick (using a regular expression):

Regex rgx = new Regex(@"color=\""(?<color>\S*)\""");
Match m = rgx.Match("a string description color=\"red\" type=\"unknown\"");
if (m.Success)
{
string color = m.Groups["color"].Value;
Console.WriteLine(color);
}

HTH, Jakob.

"Richard L Rosenheim" wrote:
I have some text where I need to extract some pieces from. The text will be
in a format like this:

a string description color="red" type="unknown"

In the above example, I would be looking to extract the word "red".

There's couple of ways I could approach the problem. I could use IndexOf to
search for the string 'color=' and then extract the value using the Substr
method. Or, I could use a regular expression like:
color="([a-z]|[A-Z])+"

In this case, the Match method would return the string 'color="red"'. I
could then use the Substr method to extract the string 'red'. Of course,
using a regular expression might be a little overkill in this case.

I'm just wondering if there is, buried in the 1500+ classes of .NET, a
better approach. Or, if there is a way of getting a regular expression to
return just the string within the quotes.

Richard

Jul 21 '05 #2
Richard,

As most things with dotNet there is not a "best" method.

I always write when someone ask that as you that there than would be only
one method.

Mostly is it a matter of preference or better said the knowledge of a
programmer from the classes.

I will avoid forever a regex (because I don't like any kind of cryptic
code), where others take it direct because they use it without it seems
thinking ("thinking" not related too the first part of the sentence).

I would probably go in your situation for an IndexOf and the Substring (not
the Substr that is Scripting)

Just my thought,

Cor
Jul 21 '05 #3
Thanks. It works -- just what I was looking for.

Richard
"Jakob Christensen" <jc*@REMOVEpension.dk> wrote in message
news:A4**********************************@microsof t.com...
The following C# code should do the trick (using a regular expression):

Regex rgx = new Regex(@"color=\""(?<color>\S*)\""");
Match m = rgx.Match("a string description color=\"red\" type=\"unknown\""); if (m.Success)
{
string color = m.Groups["color"].Value;
Console.WriteLine(color);
}

HTH, Jakob.

"Richard L Rosenheim" wrote:
I have some text where I need to extract some pieces from. The text will be in a format like this:

a string description color="red" type="unknown"

In the above example, I would be looking to extract the word "red".

There's couple of ways I could approach the problem. I could use IndexOf to search for the string 'color=' and then extract the value using the Substr method. Or, I could use a regular expression like:
color="([a-z]|[A-Z])+"

In this case, the Match method would return the string 'color="red"'. I
could then use the Substr method to extract the string 'red'. Of course, using a regular expression might be a little overkill in this case.

I'm just wondering if there is, buried in the 1500+ classes of .NET, a
better approach. Or, if there is a way of getting a regular expression to return just the string within the quotes.

Richard

Jul 21 '05 #4

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

Similar topics

2
4016
by: Laz | last post by:
Hi, I've not worked with javascript, and after searching could not find example of this type of manipulation. I'm trying to set a variable to the last portion of a domain name. for example,...
2
2807
by: Dickyb | last post by:
Extracting an Icon and Placing It On The Desktop (C# Language) I constructed a suite of programs in C++ several years ago that handle my financial portfolio, and now I have converted them to...
2
2527
by: DevDave | last post by:
Does anyone know of a quick method to extract just the numeric portion of a string without looping through each character? For instance: 674h23i -> 67423 23-112 -> 23112 87 (21) -> 8721...
3
227
by: Richard L Rosenheim | last post by:
I have some text where I need to extract some pieces from. The text will be in a format like this: a string description color="red" type="unknown" In the above example, I would be looking to...
13
3717
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
2
2490
by: Kevin K | last post by:
Hi, I'm having a problem with extracting text from a Word document using StreamReader. As I'm developing a web application, I do NOT want the server to make calls to Word. I want to simply...
2
4132
by: VictorTan | last post by:
Hello. I'm new to this forum. Hope that I don't make mistakes in here but if I do, please correct me if there is. Thanks. I also wanted to ask you guys regarding about the following following...
4
3759
by: dexter48 | last post by:
Hi I'm searching for a string occurance in a text file. I find the string ok and write the results to a log file. But on the line above is also some information I need. How can i get that. The string...
5
3627
by: Mukesh | last post by:
Hi, I am using framework 2.0. I am writing a foreach loop that will extract single dimensional arrays out of double dimensional array. I am trying writing something like this. string ...
0
7229
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,...
0
7129
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...
0
7398
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...
1
7061
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...
1
5057
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...
0
3208
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...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
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.