Jump to content
Sign in to follow this  
kylomas

[Solved] Return strings that do NOT match regexp pattern

Recommended Posts

kylomas

Good Afternoon,

Given the following string :

03/03/2010
1/12/2010
ee/ee/ee
10/3/1999
22/22/2222
11/11/11

\d\d/\d\d/\d\d\d\d
will give me the two matches that I expect.

Is there a way to return all lines that do NOT match the pattern?

I've tried playng around with groups and the caret char to no avail, obviously.

Thanks,

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jdelaney

best i can do:

$string = "03/03/2010" & @CRLF & _
"1/12/2010" & @CRLF & _
"ee/ee/ee" & @CRLF & _
"10/3/1999" & @CRLF & _
"22/22/2222" & @CRLF & _
"11/11/11"
$adata = StringRegExp ( @CRLF & $string & @CRLF, "sd/d+/d+|d+/d/d+|sd+/d+/d{1,3}s|s[^ds]+.+/[^ds]+.+/[^ds]+.+s", 3 )
_ArrayDisplay ( $adata )

eaiser with stringregexp replace:

$string = "03/03/2010" & @CRLF & _
"1/12/2010" & @CRLF & _
"ee/ee/ee" & @CRLF & _
"10/3/1999" & @CRLF & _
"22/22/2222" & @CRLF & _
"11/11/11"
$string2 = StringRegExpReplace ( $string, "d{2,2}/d{2,2}/d{4,4}", "" )
$adata = StringSplit ( $string2, @CRLF, 2 )
For $i = UBound ( $adata ) - 1 To 0 Step -1
 If StringLen ( $adata[$i] ) = 0 Then _ArrayDelete ( $adata, $i )
Next
ConsoleWrite ( $string2)
_ArrayDisplay ( $adata )
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
kylomas

jdelaney,

Thanks for the prompt reply!

If I understand you regexp correctly you are looking for all patterns that do not match the pattern that I want. This works for this example, however, I am auditing data prior to inclusion to a database and may not be able to id all possible BAD combinations. That is where my idea to return the NOT of matches came from.

I want to review the data that does not match what I expect to make sure that I am anticipating all cases.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

jdelaney,

Sorry, missed your edit. I think that will work perfectly.

kylomas

edit: although I was hoping not to have to do this logically with arrays.

*** WARNING ***

The Surgeon General has determined that regular expression may be hazardous to your health.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jdelaney

I just added the arrays to get rid of those remnant carriage returns...couldn't get rid of them with the regexp.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
jchd

If you need to sanitize individual data only, then use a simple StringRegExp with the required pattern and option 0. This should give you the green/red light you want.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
jchd

@jdelaney,

including a final s? in the pattern should work or am I missing something obvious?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
kylomas

@jdelaney - thanks, works perfectly

@jchd - thanks, I am doing some of that already. The case that I'm working on now is weeding out the myriad of ways that a date can be entered.

I just started on this and it seemed that returning the NOT of an expression would be handy in many cases.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jdelaney

@jchd, i would expect that too :)

even took into consideration that it was questionable s? (in case last record), no amount of manipulation could get rid of them, so i compromised on the array

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
kylomas

Jdelaney,

Your array method gets rid of the blank entries which makes the data easier to work with.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jdelaney

duh, crlf is 2 characters, not 1...here is the way without the array:

$string = "03/03/2010" & @CRLF & _
"1/12/2010" & @CRLF & _
"ee/ee/ee" & @CRLF & _
"10/3/1999" & @CRLF & _
"22/22/2222" & @CRLF & _
"11/11/11"
$string2 = StringRegExpReplace ( $string, "d{2,2}/d{2,2}/d{4,4}[s]*", "" )
$adata = StringSplit ( $string2, @CRLF, 2 )

ConsoleWrite ( $string2)

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
kylomas

jdelaney,

Thanks, perfect and almost instantaneos tested against a 50KB file. Inj retrospect I do need to see the blank entries as they indicate missing data.

kylomas

edit: brain fart from exposure to regexp

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×