The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 12:13:07 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: How do you match the adjacent word  (Read 5108 times)
hvshah69
Rookie
**
Offline Offline

Posts: 21


View Profile
« on: April 30, 2010, 03:27:25 PM »

This may be pretty obvious to some of you gurus but my regex knowledge is pretty basic.

I just want to create an expression where if I am searching for a keyword the regex function will return the keyword+adjacent-word-on-right.

In other words,

if my subject is "quick brown fox jumps over lazy dog". If my keyword is "fox", I want the regex function to return "fox jumps".

I have no knowledge of what the next word is going to be or the length of the word for that matter. What will be the regex expression for this?

Thanks in advance.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: April 30, 2010, 04:33:35 PM »

This little diddy will not work if fox is followed by a comma, period or anything other than whitespace and then the next word.

I assume PHP, so I'll write this with that function:

preg_match_all('/fox\s([A-Z]{1,32})/i', $inputStr, $parts);

$parts will then contain an array that has the word next to fox. That regex reads, "find the word fox with some white space after it. Provided it is found, and there is a string of characters containing A-Z of length 1..32 chars, collect that. The preg_match_all rolls through the inputStr to get all occurrences rather than just the first, like preg_match.

There are a whole lot of ways that you might make that more effective, not the least of which is optional punctuation.

Google Jan Goyvaerts regex for the best resource I've found on the web. Buy the book. It'll set you right, straight away (Amazon and others have it as well).

Or just go here: http://www.regular-expressions.info/tutorial.html
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
hvshah69
Rookie
**
Offline Offline

Posts: 21


View Profile
« Reply #2 on: April 30, 2010, 04:53:56 PM »

Many Thanks, Perkiset.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: April 30, 2010, 04:57:47 PM »

No worries mate.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!