Regular Expression hanging when used within .NET

Hi There All,

I am reasonably new to Regex's and have hit a peculiar problem.  I have built a Regular Expression using RegexBuddy (www.regexbuddy.com) and have got it to pass against a test string correctly.

The RegEx used is as follows:
P[ ]\d{4}[ ](?:\d|[ ]){3}[ ]\d{2}[ ](?:\w|[ ]){8}[ ](?:\d|[ ]){7}[ ]\d{8}[ ]\d{6}[ ]\d{6}[ ](?:.|[ ]){30}[ ](?:.|[ ]){35}[ ](?:\d|[ ]){8}[ ]\w{1}

A test string which will pass is as follows:
P 0001 071 14 201         1495 20050401 171224 003410 Internet                       Smith,Mr                                  15 M

The RegEx is for testing a message received via TCP to ensure it meets a specific format.

Now my delima is, that this RegEx passes ok when matched within C# .NET, however when I wish to test the character code 0x02 at the start of the RegEx and a few other character codes at the end of the RegEx, when attempting to run the IsMatch() function to match, Visual Studio Debugging hangs and it seems the RegEx is never evaluated and you have to terminate the process (by ending debug).  Please note I am using the RegexBuddy "use" tab which outputs the correct syntax for C# as all backslashes need to be doubled and line begins with \A and ends with \z.

The RegEx which fails to execute is:
\x02P[ ]\d{4}[ ](?:\d|[ ]){3}[ ]\d{2}[ ](?:\w|[ ]){8}[ ](?:\d|[ ]){7}[ ]\d{8}[ ]\d{6}[ ]\d{6}[ ](?:.|[ ]){30}[ ](?:.|[ ]){35}[ ](?:\d|[ ]){8}[ ]\w{1}\x03\x13\x10

You can see the only changes are the \x02 at the beginning of the RegEx and the \x03\x13 and \x10 at the end of the RegEx.  However please be aware that this RegEx DOES evaluate correctly using RegexBuddy with the test string I gave you above (if it does not pass in RegexBuddy for you, remove \x13 + \x10), just not in .NET, however I feel even though it is passing in RegexBuddy, it must be in bad form.

I have not given you guys a string with the extended characters added as it does not make a difference as the RegEx will not even evaluate, but if required simply just add them to the string.

This is very odd, I'm sure I must need some extra characters in the RegEx or something.
Thanks for letting me take up your time and hope you can help! =)

Graham


LVL 1
VotechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VotechAuthor Commented:
RegEx snippet without browser interference attached.
regex.txt
0
VotechAuthor Commented:
OK, it appears actually .NET isn't even finding matches with my first Regex; however RegexBuddy is!

Is there differences between the two engines?  Need to find out what is wrong with my non-character code RegEx also.

Thank you kindly.
0
VotechAuthor Commented:
My apologies, my first Regex is working in .NET now, I needed the C# code export from RegexBuddy which was 'Test if the regex matches (part of) a string' as for now I removed the character codes from the Regex.

Back to original problem now.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

ozoCommented:
(?:.|[ ]){35} may try to match in as many as 2^35 different possible ways before giving up
why not just .{35}
0
VotechAuthor Commented:
True.  as a dot matches any character, including spaces.

That helps, I will make the change.
0
VotechAuthor Commented:
Can anybody help?

It seems if I change "Smith,Mr" to "Smith0,Mr" adding one more character and attempting to make the RegEx not pass, it just hangs......
0
VotechAuthor Commented:
Wow, ozo, making that change alone seemed to have fixed everything.  I wasn't going to implement that just yet as I thought it was just an efficiency change but that was the root of all my issues.  It seemed that my RegEx was hanging whenever it could not be matched; not just on extended ASCII codes.

If nobody has any suggestions on my RegEx I will award these points in a couple of days once I can guarantee it is rock solid.

Thanks ozo.
Hopefully you can reply and let me know your thoughts.......
0
ozoCommented:
It is an efficiency change.
I expect that if you let the original RegEx run for a few trillion times as long, it will eventually report a failure to match,
but before that, it has to try to see it it can match as
........................................
.......................................[]
......................................[ ].
......................................[ ][ ]
.....................................[ ]..
.....................................[ ].[ ]
.....................................[ ][ ].
....................................[ ][ ][ ]
...................................[ ]...
...................................[ ]..[ ]
...................................[ ].[ ].
etc.
etc.
before it can finally give up and declare that there is no possible match with any combination of . matches and [ ] matches
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.