![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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+ void int |
|
|
|
|
|
#2 |
|
Programmer
|
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
|
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|$) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |