![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 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 Specs![]() |
![]() |
| 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 | |||