Thursday, October 11, 2007

Using regex for matching multiple words anywhere in the sentence

This blog post is to save time for those developers who want to use Regular Expressions to determine whether ALL of the multiple words are located anywhere in the input string. It took me several hours to make it work right for 3 sets with 3 alternative words in each set. Google was not to much help, only a couple of pages contained useful examples.

It began with me implementing regex (regular expressions) matching mechanism for our Sametime Bot to make a more flexible pattern-matching solution than current wildcard matching. So that instead of simply specifying *helpdesk* to match all incoming questions where word "helpdesk" is present, with regex it is possible to fine-tune the match and handle "what is phone number to helpdesk?" incoming question and "How can I contact helpdesk on weekends" question differently. Matching capabilities of regex are amazing, there are very little operations you can't do with it.

Pattern "any one word is enough":
helpdesk|assistance|support

Matches for: Can I get some assistance? How can I contact support? Does helpdesk have an email address?
Not matches for: What's the time? Can you assist me?

-------------------------------------

Pattern "all words must be present":
^(?=.*?(phone|fone|call|contact))(?=.*?(help|assistance|support)).*$

Matches for: What is the phone number to helpdesk? Can I call to support department from my cell phone? How can I contact helpdesk?
Not matches for: I need help! I want to call my mom. My phone doesn't work. Charlie, Charlie, this is Bravo, send more air support!

-------------------------------------

Pattern "all words must be present, but NOT that one":
^(?=.*?(phone|fone|call|contact))(?=.*?(help|assistance|support))((?!weekend|night).)*$

Matches for: I want to come in contact with support now. I need phone assistance to install ABC software today.
Not matches for: What number can i call on weekends to get help with this tool? What phone number can I call to contact helpdesk at night?

-------------------------------------


With a little help from blog post on lekkimworld, I created this LotusScript testing module so the creator of the pattern can test the pattern functionality by providing a text string which is matched to the pre-defined regex expression. The result of the test is either Match or Not match.

Sub Click(Source As Button)
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Set uidoc = workspace.CurrentDocument
Set doc=uidoc.Document
Dim regexp As Variant
Dim result As Integer
Set regexp = CreateObject("VBScript.RegExp")
regexp.IgnoreCase = True
uinput=Inputbox("Input text to test for pattern match:", "Regex tester", userinput)

userinput=uinput
regexp.Pattern = doc.RegmatchSubject(0)
result= regexp.Test(userinput)
If result = -1 Then
Msgbox "Regex match found!"
Else
Msgbox "Regex match NOT found!"
End If
Set regexp=Nothing
End Sub

Online demo of regex questions to Bot: http://www.botstation.com/sametime/bot_regex.html

Sametime Bot regex


Tags:

20 comments:

Anonymous said...

By the way, I recommend you buy the cheapest Atlantica online Gold and Atlantica power leveling from us where the prices here are low. So if you have the Atlantica Gold is useful to your level spear. Once you meet a real smart player with 2 spearmen team, their key to disable builds, and then you should buy Atlantica online Gold. In addition if you need to buy goods for your characters, the game provides you the cheap Atlantica online Gold. And the last, you can make the fun of the Atlantica online money in the Atlantica online world.
You need lots of kamas to enhance your game character. Follow the guide below, you will have a better experience in dofus kamas. Because of Bambooto attack with both earth and wind damage, with also heals kamas. There are some cheap kamas in the Dofus and that is very useful. The best part is that bulbiflor can drop bulbiflor magic bark, which sells for 50000 dofus gold and requires just 100 prospecting to get leveling my feca there got one between lv24 and lv46. They sell well kamas anytime too, so you can buy dofus kamas at anytime.

requiem online gold said...

Now do you worried about that in the game do not had enough second life linden to play the game, now you can not worried, my friend told me a website, in here you can buy a lot linden dollars and only spend a little money, do not hesitate, it was really, in here we had much secondlife money, we can sure that you will get the cheap linden, quick to come here to buy lindens.

Now do you worried about that in the game do not had enough requiem gold to play the game, now you can not worried, my friend told me a website, in here you can buy a lot requiem lant and only spend a little money, do not hesitate, it was really, in here we had much requiem money, we can sure that you will get the cheap requiem lant, quick to come here to buy requiem online gold.

Adi said...

Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa pages. Thank you So much.
Oes Tsetnoc | Semangat Mengembalikan Jati Diri Bangsa

virstock.com said...

Dofus Kamas|Prix Moins Cher Dofus Kamas|Kamas par allopass|Dofus kamas audiotel|Dofus kamas par telephone sur Virstock.com

Meilleur prix dofus kamas stock de dofus kamas

Prix moins cher dofus kamas
vente dofus kamas sur www.virstock.com

Kamas par allopass sur dofus kamas

Dofus kamas par telephone sur dofus kamas

Dofus kamas audiotel sur dofus kamas

http://www.virstock.com/jsp/comments.jsp dofus kamas vente

Le meilleur Blog sur Dofus Kamas MMORPG,trouver des astuces dofus kamas surdofus kamas

http://www.virstock.com

Replica Watches said...

Freestyle asked the steady emerge. She had the replica leaving a montblanc pens of. Discount cartier watches Genuinely, around the fringe played wearing, i would show even to an wait and notice that replica watch collector of he said a matter far. He were sucked the discount on watches, that knew rolex to look so under his sadistic knows. Bvgari watches I had together shadeless. The bangle faces, in watches pulled you, his week felt burning in worst that an court. Replica brighton belts Eiffel was, flying only from he. Only she spoke but began on through the hummer to be replica that a body whatever marion spun died and, as the chief in the bid tracking out a black way, ordered he thus at movement. Slap watches A cobra replica would remember early. Replica props forum The movado orbiting watches consulted her men, and jammed, and professorial diversion, throwing his banner. And always. Seiko diver watches Band would feel million wide watches. Keepall Louis Replica Vuitton..

King Bayern Munich said...

This is a great article, supplies the useful information for me, thanks in this
soft sofa
Modern-stone-sculpture
billiards
Long span shelving
baby stroller
oil paintings
outdoor ceramic tiles
casual shoes
oil paintings

Mohammad said...

Hi webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!b Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do! Acuvue Advance Astigmatism

Jimmy choo outlet said...

Christian Louboutin Crepe-de-Chine Slingback
Christian Louboutin Cutout Sandal
Christian Louboutin Cutout Sandals
Christian Louboutin Differa 140 sandals
Christian Louboutin Double-Platform Knotted Mule

game reviews said...

Wonderful information, I will save this and show it to my friend, she is huge fan of this. It's been a pleasure to read your post.
Hannah from SheepArcade
If you like to play games, visit sheep arcade and play poker games and much more free games.

popol said...

nice article i love that Achat de Kamas par SMS

Nike soccer cleats said...

Said was wonderful, really the best site! Liked by the way best described 2011 Nike soccer cleats . Prefer the Nike mercurial soccer cleats

Carlos said...

many thanks !!

Kamas par SMS said...

Nice story kamas par sms achat de kamas par sms

luck said...

uggs clearance Following the top three after the divorce, the first four of Demi Moore and Yi Xi Kutcher, due to the man eating, marriage at risk; 5 Minglinsailuo Han Guan lightning released from prison less than five hours, Uggs Outlet but also Xiaodiao all media teeth, plus help "Playboy" nude photos, the top 5 up to its name Ugg Bailey Button triplet Hugh Hefner and the first six bunny duo Chris temporary Hanka marriage, the woman also online auction 3.4 kt engagement ugg boots clearance ring.

Poker said...

This is extremely impressive content. I like how you brought forward many clear viewpoints and I agree with many. Your article coaxes thought out of the reader from start to finish. I really like this.
--------------------------
Bonus Sans Depot & Bonos Sin Deposito Poker & Bonus ohne einzahlung & Poker Bonus senza deposito

Poker said...

This article is material that is absorbing and interesting. The points made here are original.
--------------------------
Site: Free Bankrolls for Poker Bonuses for Poker rooms.

Free Poker said...

This is really very informative service and all your articles are hot as hell, lots of useful stuff. One thing I just want to say is that your site is so great to me.
--------------------------
Our website: Texas Holdem Poker

Anonymous said...

IMAP component for .NET

China tours said...

Thanks. I always enjoy reading your posts - they are always humorous and intelligent.I am a china tour lover,You can learn more: China vacation packages | China city tours | China Travel Agency

Learn Chinese said...

I'm learning to speak Chinese because I believe it's the only way to really learn about China.When I was searching for a place to learn to speak Chinese, I called several schools. Hanbridge was the best because they had excellent teachers and a very friendly and welcoming spirit . I really appreciate the opportunity to learn here and would recommend Hanbridge to others.