Matching VbCrLf with RegEx

John Clark
John Clark used Ask the Experts™
on
Hello Experts,

I could use some help with Regular Expressions. My problem is easy to explain:

Normally the pattern I am trying to match looks like this:

CASUALTY/INITIAL-17051/STBD FIN STAB OVERHEATS/EIC:TR00000/CAT:2//

The match syntax looks like this:
(CASUALTY.*\/.*)\/\/
Which returns:
CASUALTY/INITIAL-17051/STBD FIN STAB OVERHEATS/EIC:TR00000/CAT:2

The problem is that sometimes the message comes in with a VbCrLf somewhere in the string and it returns a null.

I tried to but \r\n in various places but nothing seems to work. Any assistance with this would be greatly appreciated!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Consulting
Distinguished Expert 2017
Commented:
Hi,

Maybe look for any character or CRLF (enclosed in non capturing group) instead ?
(CASUALTY(?:.|[\r\n])*\/(?:.|[\r\n])*)\/\/

Open in new window

John Clarklogistics specialists

Author

Commented:
Works like a charm! I tested thoroughly with a hard return in various places and they ALL return exactly what I was looking for. A thousand thanks, a thousand times over...

Would it be too much to ask to explain what those additional characters mean?
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
My best advice will be to look at an online regex tester site, such as https://regexr.com/
Everything is explained there (and much better than I will ever do).
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

John Clarklogistics specialists

Author

Commented:
Will do and thanks again!
John Clarklogistics specialists

Author

Commented:
Opps...I spoke too soon. It is grabbing everything after the escaped "//" (\/\/). I want the pattern match to stop after it finds it...
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
Guess in its actual form, the regex isn't precise enough.

You'll need to provide more details about the message's structure, so we can write a more accurate regex.
John Clarklogistics specialists

Author

Commented:
Sure thing, and thanks for sticking with this.

The complete message can look like this:

FM SOMBODY
TO SOMEBODY ELSE
REF/A/SOME OBSCURE BOOK//
MSGID/CASREP/DDG 123/123//
CASUALTY/INITIAL-17051/STBD FIN STAB OVERHEATS/EIC:TR00000/CAT:2//
1. THE PROBLEM IS THIS
//
THE END

The pattern you provided grabs returns:
CASUALTY/INITIAL-17051/STBD FIN STAB OVERHEATS/EIC:TR00000/CAT:2//
1. THE PROBLEM IS THIS
//
THE END

What I want is:
CASUALTY/INITIAL-17051/STBD FIN STAB OVERHEATS/EIC:TR00000/CAT:2

Hope this clears it up and thanks again...
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
After a quick analysis in the part you want to extract, it looks like you have:

the word CASUALTY (is that always the case ?)
followed by a slash
followed by the word INITIAL (is that always the case ?)
followed by a minus
followed by 5 numbers
followed by a slash
followed by a description (anything that isn't a slash ?)
followed by a slash
followed by 3 letters (always EIC ?)
followed by a semi-colon
followed by 2 letters (always TR ?)
followed by 5 numbers
followed by a slash
followed by 3 letters (always CAT ?)
followed by a semi-colon
followed by a number
followed by 2 slash

There are some parts wich I'm still unsure about, please let me know if I'm right, or provide a correction.
John Clarklogistics specialists

Author

Commented:
Not quite that complicated. There are really two constants here: the string "CASUALTY/" and the next "//" it encounters. I am looking for everything between (including any VbCrLf which was causing the problem) them. More specifically, I want a string that starts with "CASUALTY/" and all the elements before, but not including the "//".

For the record,I use the resulting string as a slash ("/") delimited array for populating an Access table.
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
This should do the job then:
(CASUALTY\/(?:.|[\r\n])*?)\/\/

Open in new window

John Clarklogistics specialists

Author

Commented:
I will try it first thing tomorrow morning at work. Really appreciate your help with this.
John Clarklogistics specialists

Author

Commented:
Most excellent! Gracie mille. I tried to mark the last contribution as the actual "solved" post but was unable too.

Got some reading to do now so I can figure out why yours works.

Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial