Avatar of John Clark
John Clark
Flag for Italy asked on

Matching VbCrLf with RegEx

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!
Regular ExpressionsMicrosoft Access

Avatar of undefined
Last Comment
John Clark

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Fabrice Lambert

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
John Clark

ASKER
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 Lambert

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).
John Clark

ASKER
Will do and thanks again!
Your help has saved me hundreds of hours of internet surfing.
fblack61
John Clark

ASKER
Opps...I spoke too soon. It is grabbing everything after the escaped "//" (\/\/). I want the pattern match to stop after it finds it...
Fabrice Lambert

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 Clark

ASKER
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...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Fabrice Lambert

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 Clark

ASKER
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 Lambert

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

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
John Clark

ASKER
I will try it first thing tomorrow morning at work. Really appreciate your help with this.
John Clark

ASKER
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.