Solved

VBScript Multi Line Pattern Match

Posted on 2009-05-07
16
735 Views
Last Modified: 2012-06-21
I am trying to extract a SQL statement from an email. The problem is that I don't know if the email message will have a signature line or not.  

So, in the code section I would want everything before two carriage returns in a row.

The ideal match would be...

select *
from smr_retl_cnt_stg
with ur

I tried
((?:.+\r\n)*)\r\n\r
but I wasnt getting the expected results

select *

from smr_retl_cnt_stg

with ur

 

 

Chris G x83061

----------------------------

Sent from my BlackBerry Wireless Handheld

Open in new window

0
Comment
Question by:orbitus
  • 8
  • 8
16 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 24329684
In ASP/VBSCRIPT with your sample that would be:
<%

Set regEx = New RegExp

regEx.Pattern = "^[\S\s]*?(?=\r\n(?: \r\n){2}|$)"

Set Matches = regEx.Execute(yourstring)

%>

Open in new window

0
 
LVL 4

Author Comment

by:orbitus
ID: 24329797
Hi ddrudik, I am using the VB script engine in Office VBA. Identical to the Javascript engine.  Your string does not seem to work with that engine.
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
ID: 24329824
From testing with http://regexpal.com/ the following matches your source text copied from above:
^[\S\s]*?(?=\r\n(?:\x20\r\n){2}|$)

Open in new window

0
 
LVL 4

Author Comment

by:orbitus
ID: 24329896
When I go to that site and enter all the information I see a match to the entire statement. Am I not configuring the site correctly to run the test?
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24329920
Then it is likely that your source text does not match the copy-and-paste that I took from your question.  Consider copying/pasting another example here for testing.
0
 
LVL 4

Author Comment

by:orbitus
ID: 24329922
The engines must be different. This pattern works in VBA

((?:.+\r?\n)*)\r?\n\r?\n

All I had to do to fix my own was put in the optional handling of the carriage returns.

Thanks for the help though.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24329933
If that is the case then your source differed from the sample, which had \x20\r\n in it.
0
 
LVL 4

Author Comment

by:orbitus
ID: 24329945
Using this VBA code, just posting for reference.
Dim myMatch As Match

Dim myMatches As MatchCollection

Dim myRegExp As RegExp

Set myRegExp = New RegExp

myRegExp.IgnoreCase = True

myRegExp.Pattern = "((?:.+\r?\n)*)\r?\n\r?\n"

Set myMatches = myRegExp.Execute(strSQL)

For Each myMatch In myMatches

	'matched text: myMatch.Value

	'match start: myMatch.FirstIndex

	'match length: myMatch.Length

	For I = 1 To myMatch.SubMatches.Count

		'backreference text: myMatch.SubMatches(I-1)

	Next

Next

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Author Comment

by:orbitus
ID: 24329964
What is \x20, I don't see that?
0
 
LVL 4

Author Comment

by:orbitus
ID: 24329977
Here is another sample...

select *
from smr_retl_cnt_stg
with ur
 
 
Chris G x83061
----------------------------
Sent from my BlackBerry Wireless Handheld


I was looking to get just the SQL and not the signature.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24329984
\x20 is the hexadecimal notation for the space character.
\xnn is the general syntax, here nn can be any character hex value, such as those you see in the ASCII table: http://www.asciitable.com/
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24330019
here's an example from http://regexpal.com
img.jpg
0
 
LVL 4

Author Comment

by:orbitus
ID: 24334753
Im sorry, Google Chrome and the EE site were the culprt.  The EE site keeps putting spaces on the blank lines after the SQL statement, those should not be there.  Google chrome was making regexpal not pull up any matches.

When I move over to Internet Explorer your pattern works great with the lines with spaces but not when those spaces are removed.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24335793
Have you resolved your original question?
0
 
LVL 4

Author Comment

by:orbitus
ID: 24335820
Yes, this pattern works for me...

((?:.+\r?\n)*)\r?\n\r?\n

Your pattern obviously works with the information you pulled from the site so I'm closing  the request as you solved it.  Thanks for your help.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24335829
Thanks for the question and the points.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now