Sign in to follow this  
Followers 0
gcue

possible with string reg exp?

30 posts in this topic

#1 ·  Posted (edited)

hello everyone.

im trying to find "G283H88620TS" within "MX0G283H7426288620TS"

problem is that theres no pattern on where in the string to search for..

as you can see in the case above, G283H...88620TS are in common but not the middle part..

but sometimes there is no middle part.. arrggg

what would the syntax look like?

thanks!

Edited by gcue

Share this post


Link to post
Share on other sites



hello everyone.

im trying to find "G283H88620TS" within "MX0G283H7426288620TS"

problem is that theres no pattern on where in the string to search for..

as you can see in the case above, G283H...88620TS are in common but not the middle part..

but sometimes there is no middle part.. arrggg

what would the syntax look like?

thanks!

StringInStr would be best I think.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

hey martin!

ya i tried that.. doesnt match up if theres something in the middle.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

If StringInStr AND StringInStr AND StringInStr ... etc.

as long as its below 4000 chars, you should be able to do it in one line. :)

Or you could be neat and do:

MsgBox (0, "", _StringInStrRandom ("MX0G283H7426288620TS", "G283H88620TS"))

Func _StringInStrRandom ($sSearchString, $sCharsString)
   Local $i, $Err = 0, $String = $sCharsString
   For $i = 1 To StringLen ($sCharsString)
      If Not (StringInStr ($sSearchString, StringLeft ($String, 1))) Then $Err += 1
      $String = StringTrimLeft ($String, 1)
   Next
   If $Err = 0 Then Return 1
   Return SetError ($Err, 0, 0)
EndFunc ; ==> _StringInStrRandom

MDiesel

Edited by mdiesel

Share this post


Link to post
Share on other sites

hey martin!

ya i tried that.. doesnt match up if theres something in the middle.

Where might the extra characters appear? Could they be anywhere in the string "G283H88620TS", or are you looking for the letters in that string to appear in order anywhere in the other string?

For example, would this string contain what you are searching for

"£$G{)(2%^^8$£{{3>>:<H@8**8(%620TyytrfS"


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

If StringInStr AND StringInStr AND StringInStr ... etc.

as long as its below 4000 chars, you should be able to do it in one line. :)

Or you could be neat and do:

MsgBox (0, "", _StringInStrRandom ("MX0G283H7426288620TS", "G283H88620TS"))

Func _StringInStrRandom ($sSearchString, $sCharsString)
   Local $i, $Err = 0

   For $i = 1 To StringLen ($sCharsString)
      If Not (StringInStr ($sSearchString, StringLeft ($sCharsString, 1))) Then $Err += 1
      StringTrimLeft ($sCharsString, 1)
   Next
   If $Err = 0 Then Return 1
   Return SetError ($Err, 0, 0)
EndFunc ; ==> _StringInStrRandom

MDiesel

That doesn't look like it checks for the characters in the search string to be in the order they are in $sCharsString, or am I reading it wrong?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Maybe something like this

MsgBox (0, "", _StringInStrRandom ("MX0G2H883H7426288620TS", "G283H88620TS"))

Func _StringInStrRandom ($sSearchString, $sCharsString)
   Local $i, $Err = 0
  Local $aS=stringsplit($sCharsString,''),$newS
  
  for $i = 1 to $aS[0]
      $newS &= ".*" & $aS[$i]
  Next
   $newS &= ".*"
   ConsoleWrite($newS & @CRLF)

   return StringRegExp($sSearchString,"(" & $newS & ")") 
  
EndFunc; ==> _StringInStrRandom

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I thought the idea was it could be oin any order...

you could do

Func _StringInStrRandom2 ($sSearchString, $sCharsString)

   $sChars = $sCharsString
   $String = StringSplit ($sSearchString, "")
   For $i = 1 To $String[0]
      If $String[$i] = StringLeft ($sChars, 1) Then $sChars = StringTrimLeft ($sChars, 1)
   Next
   If StringLen ($sChars) > 0 Then Return SetError (StringLen ($sChars), 0, 0)
   Return 1
EndFunc ; ==> _StringInStrRandom2

MDiesel

Edit: Whilst on this subject, Martin - I believe you posted on how to use stringregexp to find character changes, eg xxxyy would return xxx and yy. I can't seem to find it.

Edited by mdiesel

Share this post


Link to post
Share on other sites

Edit: Whilst on this subject, Martin - I believe you posted on how to use stringregexp to find character changes, eg xxxyy would return xxx and yy. I can't seem to find it.

No, I don't think that was me. It would be more likely that if I did it I would have done some sort of loop, checking the match of the next character and .. well probably I would end up with something similar to the code you posted earlier. SmOke_N is more likely to have done something like that with stringRegExp, but then so are a few hundred others!


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

hmm not sure what you mean by any order..

actually i think its not working right mdisel (might be the way i explained it)

so what im looking for is find stringA within stringB

examples where it should be found:

stringB = "JJJJJJAAAAAAAABBBBBBB123"

stringA = "AAAAAAAA123" (TRUE)

stringA = "123" (FALSE bc it still needs AAAAAAAA)

stringA = "A1A2A3AAAA" (FALSE bc stringA should be in the same sequence)

stringA never would be out of the same sequence in stringB but there MIGHT be some characters in between stringB

hope this makes sense!

thanks for your help!

Share this post


Link to post
Share on other sites

looks like this is working martin!

many many thanks!

Maybe something like this

MsgBox (0, "", _StringInStrRandom ("MX0G2H883H7426288620TS", "G283H88620TS"))

Func _StringInStrRandom ($sSearchString, $sCharsString)
   Local $i, $Err = 0
  Local $aS=stringsplit($sCharsString,''),$newS
  
  for $i = 1 to $aS[0]
      $newS &= ".*" & $aS[$i]
  Next
   $newS &= ".*"
   ConsoleWrite($newS & @CRLF)

   return StringRegExp($sSearchString,"(" & $newS & ")") 
  
EndFunc; ==> _StringInStrRandom

Share this post


Link to post
Share on other sites

try this:

#include <Array.au3>
$String = 'MX0G283H7426288620TS'
$a = StringRegExpReplace($String, '(?i)(G283H)(?:.*)(88620TS)', '\1\2')

MsgBox(0, '', $a)

Share this post


Link to post
Share on other sites

No, I don't think that was me. It would be more likely that if I did it I would have done some sort of loop, checking the match of the next character and .. well probably I would end up with something similar to the code you posted earlier. SmOke_N is more likely to have done something like that with stringRegExp, but then so are a few hundred others!

martin,

i just need it to be NOT case sensitive.. then it will be perfect!

Share this post


Link to post
Share on other sites

ombra, i cant predict when theres going to be chars in the middle or not =/

thanks!!

try this:

#include <Array.au3>
$String = 'MX0G283H7426288620TS'
$a = StringRegExpReplace($String, '(?i)(G283H)(?:.*)(88620TS)', '\1\2')

MsgBox(0, '', $a)

Share this post


Link to post
Share on other sites

martin,

i just need it to be NOT case sensitive.. then it will be perfect!

I think you just need to change the last line in the function to

return StringRegExp($sSearchString,"(?i:" & $newS & ")")

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Martin your code isnt actually founding the chars in the string

btw here is my code:

$String = 'MX0G283H7426288620TS'
$Chars = 'G283H88620TS'
MsgBox(0, '', _SearchInString($String, $Chars))
Func _SearchInString($String, $Char)
    Local $Pattern = '(?i)(?:.*)', $Replace, $nChars, $Result
    $nChars = StringSplit($Char, '')
    For $i = 1 To $nChars[0]
        $Pattern &= '(' & $nChars[$i] & ')(?:.*)'
        $Replace &= '\' & $i
    Next
    $Result = StringRegExpReplace($String, $Pattern, $Replace)
    Return $Result
EndFunc  ;==>_SearchInString

Share this post


Link to post
Share on other sites

what are you trying to return, if a match is found...

#include <Array.au3>
$String = 'MX0G283H7426288620tS'
$a = StringRegexp($String, '(?i)(g283h).*(88620ts)')

if $a=1 then 
MsgBox(0,"","found both g283h & 88620ts",0)
Else
    MsgBox(0,"","Not found sorry",0)
endif

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

what are you trying to return, if a match is found...

#include <Array.au3>
$String = 'MX0G283H7426288620tS'
$a = StringRegexp($String, '(?i)(g283h).*(88620ts)')

if $a=1 then 
MsgBox(0,"","found both g283h & 88620ts",0)
Else
    MsgBox(0,"","Not found sorry",0)
endif
no you are wrong, $a = the new string

EDIT: sorry I read StringRegexpReplace instead of StringRegexp

Edited by oMBRa

Share this post


Link to post
Share on other sites

Almost, on success $a = array type, on failure $a = nothing.

Share this post


Link to post
Share on other sites

yes I know, I read wrong, I edited my last post

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