Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - RE: searching for a pattern in a string!!

Reply
 
Old 11-14-2008   #1 (permalink)
sa


 
 

RE: searching for a pattern in a string!!

Hi,

I am using for example:

str = "04234329"
pattern= "40"

Code:
result=instr(str,pattern)

to search. So if I am searching for 40 and it finds 04 and says it
found 40 which is incorrect as 04 and 40 are not the same. Is there a
way around this problem. Thanks for helping

SA

My System SpecsSystem Spec
Old 11-14-2008   #2 (permalink)
sa


 
 

Re: searching for a pattern in a string!!

On Nov 14, 1:18*pm, sa <agarwasa2...@xxxxxx> wrote:
Quote:

> Hi,
>
> I am using for example:
>
> str = "04234329"
> pattern= "40"
>
> Code:
> result=instr(str,pattern)
>
> to search. So if I am searching for 40 and it finds 04 and says it
> found 40 which is incorrect as 04 and 40 are not the same. Is there a
> way around this problem. Thanks for helping
>
> SA
Actually the str is not correctly defined earlier. It is

str="030502012715290406281"
pattern="30"

So it correctly identifies 30 using the instr code. The problem is
that the str is a combination of 2 chars as shown below:
03
05
02
01
27
15
29
04 etc

So as can be seen its 03 and then 05 and its combined into 0305 and
the instr function finds 30 which is not what I want. Is there a way
around this ehre it can search in 2's only for a search pattern

SA
My System SpecsSystem Spec
Old 11-15-2008   #3 (permalink)
mayayana


 
 

Re: searching for a pattern in a string!!

If the search string is always composed of 2-character
pairs then you can do it like this:

s1 = "03040608"
s2 = "40"

i = InStr(s1, s2)
If i Mod 2 = 1 Then
MsgBox s2 & " found at offset " & i
End If

The Mod operator returns the remainder of division.
So x Mod 2 will return 0 for even numbers and 1 for
odd numbers. Since your pair always starts at an odd
number offset you can check a valid match by using
i Mod 2.

If there's a possibility of finding more than one
instance then you can use a loop. For example,
in "04011440" there's an invalid "40" at offset 2
and a valid "40" at offset 7. To find the first valid
match where there might also be invalid matches,
you can use a function like so:

'------------------------------------

s1 = "04060508"
s2 = "40"

MsgBox GetOffset(s1, s2)

Function GetOffset(s1, sToFind)
Dim Pt1, Pt2
Pt1 = 1
Pt2 = 0
GetOffset = 0
Do
Pt2 = InStr(Pt1, s1, sToFind)
If Pt2 = 0 Then Exit Function
If (Pt2 Mod 2 = 1) Then
GetOffset = Pt2
Exit Function
End If
Pt1 = Pt2 + 1
If Pt1 >= Len(s1) Then Exit Function
Loop
End Function

'-------------------------------

Apparently you can also use RegExp. Personally I
find RegExp to be very tedious, and overkill in the vast
majority of cases. (Such as the case at hand.) But it
seems to be a love/hate kind of thing. Some people hate
RegExp while other people like to use them in any function
where they can be shoehorned in. I suspect the RegExp
lovers are the same people who like to work out 85-character
Perl expressions that will format a disk, take out the
rubbish and wash the dishes, all in a matter of milliseconds.

Quote:

> Hi,
>
> I am using for example:
>
> str = "04234329"
> pattern= "40"
>
> Code:
> result=instr(str,pattern)
>
> to search. So if I am searching for 40 and it finds 04 and says it
> found 40 which is incorrect as 04 and 40 are not the same. Is there a
> way around this problem. Thanks for helping
>
> SA
Actually the str is not correctly defined earlier. It is

str="030502012715290406281"
pattern="30"

So it correctly identifies 30 using the instr code. The problem is
that the str is a combination of 2 chars as shown below:
03
05
02
01
27
15
29
04 etc

So as can be seen its 03 and then 05 and its combined into 0305 and
the instr function finds 30 which is not what I want. Is there a way
around this ehre it can search in 2's only for a search pattern

SA


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to prepend a filename with a pattern string? VB Script
Searching all files in a folder for a string. Vista file management
String pattern PowerShell
problems with $var | select-string -pattern $string -q PowerShell
Select-String pattern quirk??? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46