473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

regular expression problem

Hi,

Does anyone know if it's possible to use regular expressions to grab
all content between two words. For example....

------
This is some example text to illustrate my problem and here's another
my for you.
------

I want to grab all text between the words 'some' and 'my' with the
correct result being ' example text to illustrate '

The following doesn't work with either ereg or preg_match...

some([^(my)]+)my

Anyone got any ideas? Is this even possible with regular expressions?

Thanks in advance,

Paul Maunders
www.fubra.com

Nov 3 '05 #1
3 1359
In article <11**********************@g49g2000cwa.googlegroups .com>,
"Paul" <pa***********@gmail.com> wrote:
Hi,

Does anyone know if it's possible to use regular expressions to grab
all content between two words. For example....

------
This is some example text to illustrate my problem and here's another
my for you.
------

I want to grab all text between the words 'some' and 'my' with the
correct result being ' example text to illustrate '

The following doesn't work with either ereg or preg_match...

some([^(my)]+)my

Anyone got any ideas? Is this even possible with regular expressions?


Of course. :)

#!/usr/bin/php
<?
$string = "This is some example text to illustrate my problem and here's
another my for you.";

if (preg_match("/some(.*?)my/", $string, $m)){
print $m[1];
}
?>


--
Sandman[.net]
Nov 3 '05 #2
Paul wrote:
Does anyone know if it's possible to use regular expressions to grab
all content between two words. For example....

------
This is some example text to illustrate my problem and here's another
my for you.
------

I want to grab all text between the words 'some' and 'my' with the
correct result being ' example text to illustrate '
depends what you mean by 'word', a term which has various meanings.

1 approach to match what's between what's sometimes called orthographic
words could be:

`\bsome\b(.*?)\bmy\b`

the same as Sandman's but with assertions that the sequences <some> and
<my> are in fact orthographic words, that is, at both ends of each are
"word" boundaries (as defined by PHP). without these assertions the
sequence <my> appearing before the word <my> would throw the match.
The following doesn't work with either ereg or preg_match...

some([^(my)]+)my


because it's a match for <some>, then one or more of any character except
'(', 'm', 'y', or ')' until the last occurrence of <my>.

character classes are not ordered, they're lists of individual characters.

--
Jock
Nov 3 '05 #3
John Dunlop wrote:
Paul wrote:
some([^(my)]+)my


because it's a match for <some>, then one or more of any character except
'(', 'm', 'y', or ')' until the last occurrence of <my>.

----------------------------------^^^^

meant to be 'first', the first occurrence of <my>.

--
Jock
Nov 3 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: | last post by:
Hi I need to write one regex to read all the fields from the following lines / file format line 1 - some_alphanumeric,some_alphanumeric,"something, something",numbers_hyphenatedORnot line 2 -...
3
by: kieran | last post by:
Hi, I want to create a regular expression on a web form that allows the user to input a maximum of 7 characters (these characters can be either letters or numbers) but no full stops. I cant...
0
by: Mike Zhang | last post by:
I got stuck with regular expression problem. What i want to do is to extract hello world if 'message' is found in the same row. I have gotten until //A. but somehow //B doesn't work. <?php...
2
by: shomun | last post by:
Hi, I am new to regular expression stuffs. I am facing problem while implementing a reg. exp. for a textbox using regular expression validator in ASP page. Requirement: It will take only...
1
by: abranches | last post by:
Hello everyone. I'm having a problem when extracting data from HTML with regular expressions. This is the source code: You are ready in the next<br /><span id="counter_jt_minutes"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.