UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Using Regexp To Find *exact* Match?, Any Version    
 
   
theghetto
post Aug 19 2011, 07:00 PM
Post #1

UtterAccess Enthusiast
Posts: 72




Can RegExp be used to find exact matches or only patterns?


My little program opens a text file and reads in the contents to a string, then looks for a match based on a value entered in a text box.

It works great to find stuff in the text file that matches the pattern.

But if I'm searching for "abc" and the text file has both "abc" and "abcd" it will return both as a match.

Can I make it do a distinct match or should I be using a different method to search the text file?

Thank yoU!!
Go to the top of the page
 
+
rabroersma
post Aug 19 2011, 09:11 PM
Post #2

UtterAccess VIP
Posts: 1,215
From: Arcadia, California, USA



You have to use word boundaries.

I can't remember what vbscript regexp use for word boundries, but at least two of the following will work:

"\babc\b"
"\Wabc\W"
"[^a-zA-Z0-9_]abc[^a-zA-Z0-9_]"
Go to the top of the page
 
+
LPurvis
post Aug 20 2011, 04:42 AM
Post #3

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



Hi

"^abc$"

Cheers

(Oh come on - that's got to be one of my shortest ever posts... well until this bit anyway... :-s)
Go to the top of the page
 
+
theghetto
post Aug 24 2011, 11:16 AM
Post #4

UtterAccess Enthusiast
Posts: 72



Thanks for the replies.

Still having trouble. the variable strSearch holds the pattern I want to search for. So I tried:

rxSearch.Pattern = "^strSearch$"

and

rxSearch.Pattern = "^" & strSearch & "$"


but no results. What am I doing wrong do you think?
Go to the top of the page
 
+
LPurvis
post Aug 24 2011, 11:31 AM
Post #5

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



The latter should be what you seek.
What exactly isn't working with it?
Go to the top of the page
 
+
theghetto
post Aug 30 2011, 03:41 PM
Post #6

UtterAccess Enthusiast
Posts: 72



Well I borrowed this function from the Internet to do the match:

Public Function Text2String(ByVal strPath As String, _
Optional ByVal strSearch As String, _
Optional ByVal CompareMode As VbCompareMethod _
) As String
Dim strBuff As String
Dim strContent As String
Dim rxSearch As New RegExp
Dim rxMatches As MatchCollection
Dim rxMatch As Match
Dim strOutput As String
Dim strline As String * 45

rxSearch.IgnoreCase = (CompareMode = vbTextCompare)
rxSearch.Global = True
rxSearch.MultiLine = True
rxSearch.Pattern = "^" & strSearch & "$"

'Here is where I tried the suggested boundary. The msgbox on the next line comes up blank. Without the boundary it works as before

MsgBox rxSearch.Pattern

Open strPath For Binary As #1
strBuff = Space(LOF(1))
Get #1, , strBuff
If strSearch = vbNullString Then
strOutput = strBuff

Else
Set rxMatches = rxSearch.Execute(strBuff)
For Each rxMatch In rxMatches

If Me.OrdNum.Text = rxMatch.Value Then
MsgBox rxMatch.Value & vbCr & Me.OrdNum.Text

strOutput = strOutput & rxMatch.Value


Get #1, rxMatch.FirstIndex, strline
stroutput2 = stroutput2 & vbCr & strline
End If

Next
End If

Text2String = strOutput

Close #1

End Function
Go to the top of the page
 
+
LPurvis
post Aug 30 2011, 05:41 PM
Post #7

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



There are still bits in the code it's impossible to verify... Such as the reference to
Me.OrdNum.Text
Which would demand that OrdNum has the focus of course.

I'm also unclear on what the subsequent Get is intended to be for. Are you not matching on the whole Text file context in one go?

Cheers.
Go to the top of the page
 
+
theghetto
post Aug 30 2011, 06:11 PM
Post #8

UtterAccess Enthusiast
Posts: 72



Thank you for your help!


Oh right.

The user is entering their search string into the ordnum.text text box.

I was using that particular msgbox to see what the ordnum.text had compared to the the match value for troubleshooting purposes. That was how I realized it was doing the wildcard match. The msgboxes in the function normally wouldn't be there. The subroutine below passes the contents of that textbox to strSearch in the Text2String function when you press a button.

I'm trying to search the text file for duplicates and prompt the user if they want to continue even if duplicates exist. The second Get is to collect the whole record where the matches occured and output them to the user so they can see the entirety of the records. I store that get in a public string stroutput2 so that I can use it in the next subroutine. This whole thing works great for the most part.

The text file contains stuff like this. And the user is typing in the last six digits of that "QT" number.

QT0000000243024,True,True,8/19/2011 8:40:17 AM
QT0000000243025,True,True,8/19/2011 8:47:30 AM
QT0000000243026,True,True,8/19/2011 8:47:44 AM
QT0000000243027,True,True,8/19/2011 8:49:11 AM


the Ordnum.Text is being used like so:


Private Sub PrintNow_Click()
Dim intresponse As Integer

If Not (Text2String("c:\test.txt", OrdNum.Text) = "") Then
intresponse = MsgBox("Quote " & Me.OrdNum.text & " has already been sent." & vbCr & stroutput2 & vbCr & vbCr & "Send anyway?", vbYesNo + vbQuestion, "Duplicate Quote")
If intresponse = vbNo Then
stroutput2 = " "
Exit Sub
End If
End If
stroutput2 = " "

....... more unrelated code ......text file gets updated.....reports get printed....

This post has been edited by theghetto: Aug 30 2011, 06:20 PM
Go to the top of the page
 
+
LPurvis
post Aug 30 2011, 07:32 PM
Post #9

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



>> the user is typing in the last six digits of that "QT" number

So you don't want a complete match. (^$ forces the pattern to match the entire search string.)
You're wanting to match on what...?

"QT0000000" & strPattern & ","
Go to the top of the page
 
+
theghetto
post Aug 31 2011, 11:37 AM
Post #10

UtterAccess Enthusiast
Posts: 72



Sorry I left out some detail.

When the user opens the form, the text box gets pre-loaded with QT0000000. The user then types in the last 6 digits. I do want to match on the entire 15 digits.

Thank you,
Go to the top of the page
 
+
LPurvis
post Aug 31 2011, 11:45 AM
Post #11

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



Yes - on the entire 15 digits - but not on the whole content of the textfile.
Hence the first separated segment (before the first comma).
Go to the top of the page
 
+
LPurvis
post Aug 31 2011, 11:45 AM
Post #12

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



Essentially I don't see how you can be matching the equivalent of "abc" against an "abcd" with the formatting and search text you've mentioned since. It'll match the number you want or nothing...
Go to the top of the page
 
+
theghetto
post Aug 31 2011, 12:47 PM
Post #13

UtterAccess Enthusiast
Posts: 72



You know what, I think I'm trying to fix something that isn't really a problem after all.

You're right, it does work perfectly with the full 15 digits. Where it returns the unwanted output is when I was trying 14 digits. If I entered QT000000022222 instead of QT0000000222222, I would get a return for 22222, 222221, 222223 and so on.

I was thinking that before we rolled over to the first 100,000 quote numbers, that we were dealing with a 14 character string. We are now turning out around fifty thousand quotes per year, so I was thinking this could be an issue if someone was trying to reprint an older document.

I was wrong, there were eight leading zeroes then, and there were still 15 characters. Now I realize this problem would only happen if the user made a mistake when typing in the number.

I do want to notify the user if they fail to enter the full number correctly. I should probably just require the 15 digit input instead of trying to deal with the output being unexpected.

This post has been edited by theghetto: Aug 31 2011, 12:48 PM
Go to the top of the page
 
+
LPurvis
post Aug 31 2011, 01:06 PM
Post #14

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



Glad it's making sense.
If you were wanting to check against an optionally different entry, I'm pretty sure a more involved expression could be determined. But ultimately you'll be almost there now I imagine?

Cheers.
Go to the top of the page
 
+
theghetto
post Aug 31 2011, 02:24 PM
Post #15

UtterAccess Enthusiast
Posts: 72



Almost there, apparently there is no inputmask property in VB6 for a text box ? Is there a reference I can add that supports that property?
Go to the top of the page
 
+
LPurvis
post Sep 1 2011, 08:29 AM
Post #16

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



Not AFAIK. Not a standard control anyway. But then - I never use Input Masks in Access either - haven't for a long time.
Though theoretically instructive - they can be limiting and confusing for users. Validating control values after entry and giving constructive feedback on that is generally less intrusive.
Go to the top of the page
 
+
theghetto
post Sep 1 2011, 03:49 PM
Post #17

UtterAccess Enthusiast
Posts: 72



ok got it (IMG:style_emoticons/default/smile.gif) Now when they press the print button if the quote number is not exactly 15 characters it prompts them to recheck. Rolled it out to users already and it is working great so far. Thank you for all the help!!
Go to the top of the page
 
+
LPurvis
post Sep 2 2011, 04:58 AM
Post #18

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



No problem. I don't think I actually provided anything in the end other than "you're already there" and "no". :-p

Cheers.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 21st May 2013 - 01:07 PM