Anyone good with regular expressions?

Need help with your JScripts? Got questions concerning the DFNs? Come forward, step inside :)
Post Reply
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Anyone good with regular expressions?

Post by Xuri »

I'm trying to do a search in a speech string for the name of a character, more specifically pUser.name.

If I wanted to search for a specific name I could do
if( Speech_Array[currObj].match( /\bWilly\b/ ))
..which would find all instances of Willy in the speech even if there'd be a question mark or exclamation mark behind.

But since I need to search for what can be ANY name, i.e. pUser.name, I need to include pUser.name in the regular expression somehow. I just have no idea how to do that and still make it work :(

Tried doing stuff like /\b/,pUser.name,/\b/ - but that won't find names which are trailed by exclamation marks or question marks or dots.

Anyone have a clue? :P
-= Ho Eyo He Hum =-
punt
VIP
Posts: 244
Joined: Wed Mar 24, 2004 7:46 pm
Has thanked: 0
Been thanked: 9 times

Post by punt »

If I understand your correctly, you want this behavior:
Xuri wrote:If I wanted to search for a specific name I could do
if( Speech_Array[currObj].match( /\bWilly\b/ ))
..which would find all instances of Willy in the speech even if there'd be a question mark or exclamation mark behind.
But with a variable for the "Willy".

Why not create a character string that contains :
var str = "\b" + puser.name + "\b" ;

and then do a
match(str)


?
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Well, I've tried the following, both..
var nameStr = "/\b" + myPlayer.name + "\b/";
myPlayer.SysMessage( nameStr );
and..
var nameStr = "\b" + myPlayer.name + "\b";
myPlayer.SysMessage( nameStr );

The first attempt prints a single "/" as the system-message, the second prints nothing at all. :|

(And yes, a simple myPlayer.SysMessage( myPlayer.name ) does print my name in the client, so the name exists.)
-= Ho Eyo He Hum =-
punt
VIP
Posts: 244
Joined: Wed Mar 24, 2004 7:46 pm
Has thanked: 0
Been thanked: 9 times

Post by punt »

do you just want to see if the variable contiaining a name is in a string, no matter what is before or after it ?
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Well I could use SubStringSearch - or I could just plain do if (speech_array == pUser.name ) - but that would be messed up if there are exclamation marks or question marks or dots trailing the name, or if the name (or the string i'm generally looking for, not necessarily a name) is also a part of a different word altogether.

For instance, I want it to grab the "Mike" part from "Hello Mike!" - but using standard string-searches doesn't find that unless I specifically look for "Mike!". Also, I don't want the script to trigger on "hi" when someone uses the word "hill".

An example of how I'm setting up the string-searches in cases where I don't use variables:

Code: Select all

if( Speech_Array[currObj].match( /\bhail\b/ ) || Speech_Array[currObj].match( /\bhi\b/ ) || Speech_Array[currObj].match( /\bhello\b/ ))
-= Ho Eyo He Hum =-
mrboris
UOX3 Neophyte
Posts: 28
Joined: Wed Jun 30, 2004 4:00 am
Has thanked: 0
Been thanked: 0
Contact:

Post by mrboris »

using onSpeech maybe.

function onSpeech( myString, myPlayer, myNPC )

namestr = "Hail ";

if( SubStringSearch( myString, "hail" ) || SubStringSearch( myString, "Hail" )) {

namestr+=myPlayer.name;

myNPC.TextMessage(namestr);

}
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Well boris, the script already uses onSpeech. But the example you have there will make the script trigger on words like "hailstorm", or other words which has the letters "hail" as a part of it.

I want to avoid that, and the script already takes care of those instances where I'm looking up specific trigger words.

But if I want to check if a player mentions the name of an NPC that's listening to his speech, I can't look for specific trigger words, I have to look for the name of that NPC using myNPC.name or whatever.

There's a simple way of doing that - but which will make the script trigger on words that may accidentally contain the "name" of the NPC. The name "anne" is contained in the word "annex" for instance. To avoid that, I need to be able to specify myNPC.name (or a variable containing that name) in the part of the script where I look up the various words the speech consists of.

Example:
You go up to an NPC and say "Well hello there, Anne!".
Now I could have a trigger word in the script called "anne", and look for it using SubStringSearch( myString, "anne" ) - and it should work reasonably well. But if I then go up to the NPC and say "Do you know where the annex is?" - the script will find the "anne" trigger word in my speech (as a part of "annex"), and trigger the NPC response from that.

To avoid that it seems I must use regular expressions to search for "whole" words. Enclosing the trigger word in \b \b for instance, will make the script only "find" instances of the trigger word where there's nothing infront of it (or an empty space), or if there's a ending behind it (like a comma, or an exclamation mark).

So to look for a trigger word called "anne", I'd use:
SubStringSearch( myString, /\banne\b/ ) - that is, if SubStringSearch can handle regular expressions (no idea, i'm using a different method of searching the speech string - same result though).
-= Ho Eyo He Hum =-
punt
VIP
Posts: 244
Joined: Wed Mar 24, 2004 7:46 pm
Has thanked: 0
Been thanked: 9 times

Post by punt »

Xuri wrote:Enclosing the trigger word in \b \b for instance, will make the script only "find" instances of the trigger word where there's nothing infront of it (or an empty space), or if there's a ending behind it (like a comma, or an exclamation mark).
Actually, that doesn't really do it. \b matches a world boundary. \w matches an alpha character, \W matches a nonword character. \s matches whitespace (same as \t\n\r\f), and \S matches non whitespace


So I think, if you wanted to find the name anywhere (say then put the ! next the name), you would want this:

(\s*?)(\S*?)Willey(\S*?)(s*?) This I think says, 0 or more whitespace, 0 or more characters, then Willey then 0 or more characters, then 0 or more whitespace.
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Yeah I know about that - but when I have \bhail\b the script only responds to "hail" when it's written as a stand-alone word, or has a ... nonword? character behind it.

Hail. works.
Hail! works.
Hail^ works.
Hail, knave! works.
Hailsdlkfjsdlf doesn't work.
Hail3 doesn't work.

So that part of the script actually does what I want it to ;)

And if I was looking for "Willey" specifically \bwilley\b would do the trick. But I don't know what name I'm looking for, since it's different from NPC to NPC, so I can't use specific searches like that in any case. :| So I need to find a way of either including the regular expression in the variable in a way that works, or vice versa - including a variable in a regular expression :P
-= Ho Eyo He Hum =-
mrboris
UOX3 Neophyte
Posts: 28
Joined: Wed Jun 30, 2004 4:00 am
Has thanked: 0
Been thanked: 0
Contact:

Post by mrboris »

can't you use that thing that breaks speach into an array, then loop through the array.
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

That's what I'm doing. I split the string on every space, so each array entry is a word. However, extra symbols like exclamation marks and commas and dots and question marks are included in the array entries. That's why I need the regular expression :|
-= Ho Eyo He Hum =-
punt
VIP
Posts: 244
Joined: Wed Mar 24, 2004 7:46 pm
Has thanked: 0
Been thanked: 9 times

Post by punt »

So is the issue just getting the \b into the string to match?
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Apparently, yes.
-= Ho Eyo He Hum =-
mrboris
UOX3 Neophyte
Posts: 28
Joined: Wed Jun 30, 2004 4:00 am
Has thanked: 0
Been thanked: 0
Contact:

Post by mrboris »

can you go through and check for ! @ #$%&%&)@ in the arrays and if there is matches, delete them?
mogulwraith
UOX3 Newbie
Posts: 13
Joined: Sat Mar 12, 2011 5:44 am
Has thanked: 0
Been thanked: 0

Post by mogulwraith »

*bump*

I'm looking for exactly the same info. I'll pour through the scripts again but if this issue has already been solved please post the answer here!
Post Reply