[Gambas-user] Help using gb.pcre

Rob sourceforge-raindog2 at ...94...
Tue Mar 10 21:53:08 CET 2009


On Tuesday 10 March 2009 15:44, David Villalobos Cambronero wrote:
> Hi, I need to match some regular expresions, I think I can use Gambas to
> do it, in the documentation says somethig like this:
> (?i)\b[a-z0-9._%\-]+@[a-z0-9._%\-]+\.[A-Z]{2,4}\b
> But how can I use it, any idea?

That regular expression is meant to extract a valid email address.  (It may 
not be a working email address, but it should at least be legal.)

One thing I should probably make clearer in the documentation is that due 
to the way Gambas uses character constants like \n, you need to double up 
on backslashes when you're specifying a regular expression as a constant.  
So if the user is entering it or you're reading it from a file, it might 
be a\s+(b\S+) but if you're hardcoding it into your program it needs to 
be "a\\s+(b\\S+)".  Here is a Gambas script to demonstrate (just tried it 
in my own console and it runs in 2.9, but watch out for my email client's 
word wrap.)

USE "gb.pcre"

DIM myemail AS String
DIM validemail AS String
DIM re AS RegExp

myemail = "foo at ...2101..." 

re = new RegExp(myemail, "(?i)\\b[a-z0-9\\._%\\-]+@[a-z0-9._%\\-]+\\.[A-Z]
{2,4}\\b")
validemail = re.Text
if not validemail then
        print myemail & " is not a legal email address.\n"
else
        print myemail & "\n"
end if

Rob




More information about the User mailing list