Regex Text parsing

J

JaythePCguy

Hi,

I am trying to write a text parser to group all nonprintable and
control characters, spaces and space delimited words in different
groups using Regex class. Using a parsing of (?<Commands>[
\x00-\x1f\x7f-\xfe])|(?<Spaces>[ \x20])|(?<Text>[ \x21-\x7e]+) on my
sample text of \tOne\ncar red \fcar\a blue car\r\n \r\n does not work
as indetended. Specially, the spaces are grouped as commands and the
Text grouping ends up grouping words delimited by spaces. Here is the
sample output:

Found Command = 0x9
Found Text = One
Found Command = 0xA
Found Text = car red
Found Command = 0xC
Found Text = car
Found Command = 0x7
Found Command = 0x20
Found Text = blue ca
Found Command = 0xD
Found Command = 0xA
Found Command = 0x20
Found Command = 0xD
Found Command = 0xA

As you can see, the spaces are found in the 2nd text group found (car
red) as well as the last text group. Spaces also appear in the command
group (0x20 is the hex code for a space). What is the correct parsing
string to achieve what I am looking for?

Thanks,
Jay
 
K

Kevin Spencer

There's a space in your <Text> group (Just before "\x21"):

(?<Text>[ \x21-\x7e]+)

... should be

(?<Text>[\x21-\x7e]+)

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top