Advanced string comparison?

BlitzMax Forums/BlitzMax Programming/Advanced string comparison?

JoshK(Posted 2006) [#1]
Anyone have a lib for advanced string comparisons with wildcards, i.e. "Blit*asic=Blitz Basic" would return True?


Dreamora(Posted 2006) [#2]
Axe.LUA

Lua is extremely powerfull on string comparision.


grable(Posted 2006) [#3]
This is a good use case for regular expressions.

using a pattern like "Blit.+Basic" would feks match "BlitzBasic" "Blitz Basic"

I ported (if you can even call it that) the TRE regexp library a while back. So you could try it out, and see if its what you need.

TRE: http://laurikari.net/tre/
LIB: http://grable0.googlepages.com/tre_regex.rar

i didnt bother making it a module btw.


Brendane(Posted 2006) [#4]
I also made a module out of the Tiny regular expresssion parser - with a very simple interface. However, the t-rex code does seem to have a couple of issues (bugs).. it's nothing that can't be worked around by altering the expression however. I'll document and release it within the next couple of days if anyone is interested.


JoshK(Posted 2011) [#5]
I am interested.


Muttley(Posted 2011) [#6]
Brucey has a regex module as well: http://code.google.com/p/maxmods/wiki/RegExModule


JoshK(Posted 2011) [#7]
Thanks. I don't understand how that module does a wildcard seach. All the examples are just search and replace of some kind.

Here's what I want:
	Function StringContains:Int(searchstring:String,searchterm:String)
		Local regex:TRegEx=TRegEx.Create(searchstring)
		If regex.find(searchterm) Return True Else Return False
		'Return searchstring.contains(searchterm)
	EndFunction



JoshK(Posted 2011) [#8]
Ah, just do this:
	Function StringContains:Int(searchstring:String,searchterm:String)
		Local regex:TRegEx
		
		searchterm=searchterm.Replace("*",".*")
		regex=TRegEx.Create(searchterm)
		If regex.find(searchstring) Return True Else Return False
		'Return searchstring.contains(searchterm)
	EndFunction


More advanced functionality can be found here if you need:
http://www.rgagnon.com/javadetails/java-0515.html


xlsior(Posted 2011) [#9]
Regular expressions are really powerful, but they may take a while to wrap your brains around what all you can do with them.

For example, here's one I did to validate whether a string contains a valid roman numeral:
Local RegEx:TRegEx = TRegEx.Create("(([IXCM])\2{10,})|[^IVXLCDM]|([IL][LCDM])|([XD][DM])|(V[VXLCDM])|(IX[VXLC])|(VI[VX])|(XC[LCDM])|(LX[LC])|((CM|DC)[DM])|(I[VX]I)|(X[CL]X)|(C[DM]C)|(I{2,}[VX])|(X{2,}[CL])|(C{2,}[DM])")


(This one was written for the bah.regex module, don't know if it works in the others mentioned here)


Perturbatio(Posted 2011) [#10]
The regex cheat sheet here has helped me on more than one occasion:
http://www.addedbytes.com/cheat-sheets/


xlsior(Posted 2011) [#11]
Anyway, regex is good to keep in mind for other purposes to, where they are much more powerful than the old-fashioned * and ? wildcards:

- Validate an email address
- Validate a creditcard number
- Validate an IP address

(Of course you only validate the syntax, not the actual truthfulness of the information, but it's still a great way to prevent bad input in your programs)


ziggy(Posted 2011) [#12]
Yes, I'm using RegEX all the time in BLIde and Jungle Ide and in most of the parsers I've written for coding languages. I can't think of anything more flexible than this!