Regex vs Instr

x=( INSTR( mystring,"X") > 0 )

returns true or false if “X” is in mystring or not…

what I need (and I know RegEx can do it, I just can’t get around RegEx syntax)

x=DoesContain(mystring,"WXYZ")

which returns true or false if mystring contains ANY of the characters “W”,“X”,“Y” or “Z”

[WXYZ]

[ ] in a regex means “in this set”

http://documentation.xojo.com/index.php/RegEx
[a-z0-9] Matches any single character of set.

Public Function DoesContain(source as string,find as string) as boolean
  Dim rg As New RegEx
  // need to escape "find" string in some cases
  rg.SearchPattern = "["+find+"]"
  return (rg.Search(source)<>Nil)
End Function

Or…

word|W|X|Y|Z

Rather than using a set which will check for ONLY those characters. The above construct will search for “word”, “W”, “X”, “Y” or “Z”.

[quote=338458:@Norman Palardy][WXYZ]

[ ] in a regex means “in this set”

http://documentation.xojo.com/index.php/RegEx
[a-z0-9] Matches any single character of set.[/quote]

Yer welcome dave

Remember too that RegEx can be case sensitive or case insensitive. You control that by assigning RegExOptons .

You can also do a pattern like [WXYZwxyz]

Be careful with this. If your set contains a closing bracket, hyphen, or backslash, you’ll have to escape those.

The thick plottens!

and if this is to parse the sql in your database app, then regex will be mandatory at some time soon…
Instr will never be enough.

[quote=338471:@Jean-Yves Pochez]and if this is to parse the sql in your database app, then regex will be mandatory at some time soon…
Instr will never be enough.[/quote]
neither regex or instr is a tokenizer &/or parser which is really the right thing to have to recognize & handle a language
but thats way off this topic

and not to worry, I have previously developed a very fast and very powerful “tokenizer” that I have used not only in Tadpole, but in various interpeter and compiler projects I have worked on over the years… And yes I know there is a difference between a tokenizer and a parser… A tokenizer determines characters that belong to a word, phrase or quoted string, while a parser would determine the meaning or relation of those tokens to an end result :slight_smile:

[quote=338461:@Dave S] Public Function DoesContain(source as string,find as string) as boolean Dim rg As New RegEx // need to escape "find" string in some cases rg.SearchPattern = "["+find+"]" return (rg.Search(source)<>Nil) End Function [/quote]

Seems like a great function, but its returning true for everything I throw at it. Am I not understanding the purpose. I thought it was to see if a string existed inside another. If so return true else false.

No… if you want to find if a string exists inside another string that is what the INSTR function is for…
What I wanted, was to find is ANY one of a series of characters existing in a string"

DoesContain("ABCDEFGHI","ZQIX")=true // BOTH strings contain the letter "I"

to do that with Instr would be

If Instr("ABCDEFGHI","Z")=true or _
 Instr("ABCDEFGHI","Q")=true or _
 Instr("ABCDEFGHI","I")=true or _
 Instr("ABCDEFGHI","X")=true then

[quote=338749:@Dave S]No… if you want to find if a string exists inside another string that is what the INSTR function is for…
What I wanted, was to find is ANY one of a series of characters existing in a string"

DoesContain("ABC","Z")=false DoesContain("ABCDEFGHI","ZQIX")=true // BOTH strings contain the letter "I" [/quote]

Gotcha, thanks.