Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 8th, 2012, 04:57 AM
dominoman's Avatar
dominoman dominoman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: london
Posts: 469 dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 15 h 59 m 28 sec
Reputation Power: 83
Send a message via MSN to dominoman
Using Regex to match a whole string starting and endign with a word

I am trying to match a whole string using Regular Expressions. Despite reading lots of posts I can't work out just how to do it.

For example, in
Code:
The quick brown fox jumped over the lazy dog
I want to select everything between brown and lazy, i.e. I want to get

Code:
brown fox jumped over the lazy


I am using regex of:
Code:
(brown).*(lazy)


Is that right? Is there a more efficient way?

Thanks in advance for any help. I am a regex newbie and pulling my hair out!
__________________
Interested in online gaming? Access full reviews of all UK Online casinos here.

Roulette Strategy |William Hill | Learn about Wine | Jackpot joy

Reply With Quote
  #2  
Old March 8th, 2012, 11:35 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,689 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 3 h 28 m 41 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Normal string functions are generally better for this: find the offset of "brown", find the offset of "lazy" after it, and grab everything in between them.

But in terms of regular expressions, yeah that's basically it. You might want the parentheses in a different spot and maybe a space between the words:
Code:
brown (.*) lazy

Reply With Quote
  #3  
Old March 8th, 2012, 01:58 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
Without disagreeing with requinix, I'd also suggest you consider a small tweak:

brown.*?lazy
The question mark after the star ensures that you only match characters between brown and the first lazy (if you had several), as in:
That brown snake is a lazy snake, if I ever saw a lazy snake.
Without the question mark, the regex matches:
That brown snake is a lazy snake, if I ever saw a lazy snake.
__________________
Regex Tutorial | Latest RegexBuddy Demo

Reply With Quote
  #4  
Old March 10th, 2012, 04:12 AM
dominoman's Avatar
dominoman dominoman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: london
Posts: 469 dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level)dominoman User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 15 h 59 m 28 sec
Reputation Power: 83
Send a message via MSN to dominoman
Quote:
Originally Posted by ragax
Without disagreeing with requinix, I'd also suggest you consider a small tweak:

brown.*?lazy
The question mark after the star ensures that you only match characters between brown and the first lazy (if you had several), as in:
That brown snake is a lazy snake, if I ever saw a lazy snake.
Without the question mark, the regex matches:
That brown snake is a lazy snake, if I ever saw a lazy snake.


Many thanks for the advice. I tested those and they work perfectly in the online regex tester, they don't work for WP-o-matic. It matches the whole article

WP-o-matic is a great tool but it doesnt do what it should with regular expressions.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Using Regex to match a whole string starting and endign with a word

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap