Solved

C# Regex Help.

Posted on 2011-03-17
9
347 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
I'm a big fan of Windows' offline folder caching and have used it on my laptops for over a decade.  One thing I don't like about it, however, is how difficult Microsoft has made it for the cache to be moved out of the Windows folder.  Here's how to …
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

630 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