Solved

VBScript Multi Line Pattern Match

Posted on 2009-05-07
16
748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…
Suggested Courses

636 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