Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Regex Help.

Posted on 2011-03-17
9
Medium Priority
?
354 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
  • 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
 
LVL 1

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
Independent Software Vendors: 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

783 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