Link to home
Start Free TrialLog in
Avatar of whorsfall
whorsfallFlag for Australia

asked on

C# Regex Help.

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
Avatar of crysallus
crysallus
Flag of Australia image

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.
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.
Avatar of whorsfall

ASKER

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

Thanks again,

Ward
ASKER CERTIFIED SOLUTION
Avatar of crysallus
crysallus
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kaufmed
__ 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

@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.
@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  = )
@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.
>>  It kind of bothers me, because there must be a way to do it in regex.
(?<=<"?(?=.(?<!")))[^<>]*?(?="?>)

Open in new window