Solved

C# Regex Help.

Posted on 2011-03-17
9
342 Views
Last Modified: 2013-12-17
Hi,

I am looking to get some C# code that can parse a string
using a regular expression and read strings between the
<> 

So for example given the following strings:

PROPERTY <"Server Remote Name"><test.local><><0>
PROPERTY <IsMulticast><><><1>
PROPERTY <IsFileStreaming><><><1>
                        
So here are the strings that would be returned for each line passed in:

"Server Remote Name","test.local","","0"
"IsMulticast","","","1"
"IsFileStreaming","","","1"

Thanks,

Ward
0
Comment
Question by:whorsfall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 8

Expert Comment

by:crysallus
ID: 35154805
Where you've got "Server Remote Name" between the <>'s, do you want the double quote characters excluded in that instance, or included.

It's just that when you gave your returned output all quoted with double quotes, you didn't include the double quote within that string i.e. ""Server Remote Name"". I wasn't sure if that was deliberate or not.
0
 
LVL 8

Expert Comment

by:crysallus
ID: 35154825
private List<string> ExtractLines(string text)
{
	var matches = Regex.Matches(text, @"(?<=<)[^<>]*(?=>)");
	var matchList = new List<string>();
	foreach (Match m in matches)
	{
		matchList.Add(m.Value);
	}
	return matchList;
}

Open in new window

This method returns all such matches in the List<string>. It includes the double quotes in the returned string re the above. If you want that removed, let me know and I'll see what I can do to exclude them.
0
 

Author Comment

by:whorsfall
ID: 35156287
Thanks for the answer if I could get the quotes excluded that would be great :)

Thanks again,

Ward
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Accepted Solution

by:
crysallus earned 500 total points
ID: 35156605
Easier to do it with C# then in the regex.

private List<string> ExtractLines(string text)
{
	var matches = Regex.Matches(text, @"(?<=<)[^<>]*(?=>)");
	var matchList = new List<string>();
	foreach (Match m in matches)
	{
		if (m.Value.Length > 1 && m.Value[0] == '\"' && m.Value[m.Value.Length - 1] == '\"')
			matchList.Add(m.Value.Substring(1, m.Value.Length - 2));
		else
			matchList.Add(m.Value);
	}
	return matchList;
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35160874
__ NO POINTS__

>>  Easier to do it with C# then in the regex.

I would be inclined to disagree  = )
var matches = Regex.Matches(text, @"(?<=<""?)[^<>]*(?=""?>)");

Open in new window

0
 
LVL 8

Expert Comment

by:crysallus
ID: 35161378
@kaufmed: I tried approaches very similar to that and couldn't get satisfactory results. I've been testing on http://regexhero.net/tester/, and what you've provided seems to still match the double quotes. I also couldn't figure out an easy way to say to ignore double quote char's only if they exist at the start AND the end, not just one or the other in which case I suspect it should be left in.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35161476
@crysallus

That's what I get for shooting from the hip, I guess! You're correct, it doesn't work for me either. I posted quickly before leaving work and I didn't test.

I know why it doesn't work, but it would probably cloud the issue to describe it. If you could guarantee there were no quotes in the "string", then you could add quote to the list within the brackets and the pattern would work. Otherwise, some lookaround magic could be performed to make my suggestion work. As you said, though, it might be easier (more understandable) to do it in C# itself  = )
0
 
LVL 8

Expert Comment

by:crysallus
ID: 35161524
@kaufmed

Yeah. The first double-quote matches the non-lookaround part before matching with the optional double-quote in the lookaround. That was my understanding anyway. I also tried as you said, exclude the double quote within the square brackets, but saw the same problem as you if there's a double quote in the middle.

It kind of bothers me, because there must be a way to do it in regex. But when it's just being run from C#, some simple C# logic is easier.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35161540
>>  It kind of bothers me, because there must be a way to do it in regex.
(?<=<"?(?=.(?<!")))[^<>]*?(?="?>)

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question