[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  regexp: match multiple consecutive whole words on the same line
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

regexp: match multiple consecutive whole words on the same line

It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Missingl   (2 posts)  [Biography] bio
Date Sat 15 Jul 2006 02:44 PM (UTC)  quote  ]
Message
Hi all,

i'd like to write some regular expressions which match whole (specified) words on a line. For example, the string:

You feel weaker as the power of the Strength spell wanes.

i wish to filter (mark / change colour of) all instances of:
you,as,the,of,one,on

now if i use " (you|as|the|of|one|on) ", (with keep evaluating and repeat on same line ticked) if two matching words occur next to each other, it only matches the first. if another word matches further down the string, however, it matches ok.

the reason for the spaces on either side of the regexp is so it matches the entire word. i don't want it to match, for example, the "the" in "these", and therefore mark half the word.

of course one could capture all the words in the line, and parse it via script, and cause the output to popup else where, but i think speedwise the trigger only would be a tad quicker.

finally, awesome client, best i've come across yet, excellent work Nick.

thanks for any help.

m-L
[Go to top] top

Posted by Shadowfyr   USA  (1,776 posts)  [Biography] bio
Date Reply #1 on Sat 15 Jul 2006 07:18 PM (UTC)  quote  ]

Amended on Sat 15 Jul 2006 07:19 PM (UTC) by Shadowfyr

Message
Umm.. Well, the reason for this is simple. You're telling it to do this:

string = " on on one "
Search 1 = " on on one "
            ^  ^
Search 2 = "on one "
              ^   ^

Where ^ is where it finds the start and end of the next "match".


In other words, since you are specifically looking for the space, its incrementing the point where it starts looking again "past" that space. This should fix it (uses no spaces):

\b(you|as|the|of|one|on)\b

The explaination given in Mushclient's documentation is from one of those horrible, "If you are reading this, you should understand what I mean.", documents you often get in the world of "professionals", who all speak Computer Nerd. Apparently I need to take a course on it, since I can't comprehend half the more obscure stuff in the Regexp documents. lol

Just to make sure I was understanding it though, I googled for a better explaination:

http://www.regular-expressions.info/wordboundaries.html

"\b" is basically an assertion that the following match "must" have one character on either side be a \w (letter), while the other "must" be a \W (spaces, newline, etc.). It does not include the characters it finds in what it returns, so it should only match the word itself, meaning that the next search will start with the space following the word it found.

main {
__if (Schrodinger_Cat is Alive or version >= "XP"){
____if version = "Vista" then Performance /= Number_of_Cores;
____call Functional_Code();}
__else
____call Crash_Windows();}
[Go to top] top

Posted by Nick Gammon   Australia  (19,605 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sat 15 Jul 2006 09:50 PM (UTC)  quote  ]
Message
My forum post about regular expressions explains in quite a bit of detail:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=5089

Shadowfyr is right, you want an "assertion" (which does not actually consume characters) rather than a match.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Missingl   (2 posts)  [Biography] bio
Date Reply #3 on Sun 16 Jul 2006 02:21 PM (UTC)  quote  ]
Message
thanks guys,

that was a little too easy for you, i'm kinda embarrassed. Nonetheless, what you suggested worked perfectly shadowfyr, thank you.

Also thanks for the link Nick, looking through the forums i missed it. i'll be sure to have a look at it thoroughly.

Cheers,

m-L
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


2,361 views.

It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Web site powered by FutureQuest.Net]