Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 1st, 2007, 12:15 AM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Keywords regex

I'm having trouble coming up with a simple regular expression. I want a regex that will match a list of keywords, which must be surrounded by non-word characters (\W+). However, if two keywords are next to each other, the \W at the end of the first is "used up" by the first keyword and causes the next keyword not to match because of the \W at the beginning. For example:

\W+(void|int)\W+
will match only "void" in the string

void int
Any ideas? I am using PCRE. Is there a flag I can set, or something else I can do?
titaniumdecoy is offline   Reply With Quote
Old Oct 17th, 2007, 5:14 PM   #2
glimmy
Programmer
 
glimmy's Avatar
 
Join Date: May 2005
Location: Minnesota
Posts: 42
Rep Power: 0 glimmy is on a distinguished road
Send a message via AIM to glimmy
Re: Keywords regex

Perhaps using the global flag would help, and maybe using the word boundary anchors (\b) instead. They start the pattern matching at the beginning of the word instead. How you capture the result can also affect the results.

For Example:
/\b(void|int)\b/g

Last edited by glimmy; Oct 17th, 2007 at 5:18 PM. Reason: changed delimiters
glimmy is offline   Reply With Quote
Old Oct 17th, 2007, 5:30 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Keywords regex

Thanks, glimmy. That does the trick. As it happens, I had already found a solution to this problem which I posted here yesterday; unfortunately, it was erased this morning as result of server issues. However, I prefer your solution to mine as it is much simpler (although both work equally well). Here is mine, which uses lookaround constructs:

(?<=\W|^)(void|int)(?=\W|$)
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
RegEx problem.. cyphix PHP 4 Mar 4th, 2007 6:24 AM
Email validating regex Jimbo PHP 6 Jul 6th, 2006 2:58 PM
Using Regex Toro Java 4 Jun 22nd, 2006 11:15 AM
regex (preg_replace) parsing para PHP 2 Dec 31st, 2005 6:30 PM
Search using a few keywords raikkonen ASP 6 Nov 8th, 2005 8:15 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC