Solved

C# Regex Help.

Posted on 2011-03-17
9
338 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the answer if I could get the quotes excluded that would be great :)

Thanks again,

Ward
0
 
LVL 8

Accepted Solution

by:
crysallus earned 500 total points
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
__ 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
Comment Utility
@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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
@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
Comment Utility
@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 74

Expert Comment

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

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now