By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,713 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,713 IT Pros & Developers. It's quick & easy.

Regular expression help please (hopefully simple)

P: n/a
I've got a file with this in it:
[Event "Some string data"]

The data I'd like extracted is within the quotes: Some string data

I can read the file out and extract (using string positions) the data I'd
like but it would neater if I use a regular expression. Only problem is I've
never seen a working example of this type of extraction and am completely
new to PHP.

Thanks in advance
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Reckless" <re******@nospam.com> a écrit dans le message de news:
cj**********@titan.btinternet.com...
I've got a file with this in it:
[Event "Some string data"]

The data I'd like extracted is within the quotes: Some string data

I can read the file out and extract (using string positions) the data I'd
like but it would neater if I use a regular expression. Only problem is I've never seen a working example of this type of extraction and am completely
new to PHP.

Thanks in advance


$MyString = "Event \"Some string data\"" ;
preg_match("/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;
echo $RegArray[1] ; # should display : Some string data

--
John
www.realposition.com
Jul 17 '05 #2

P: n/a
.oO(John)
$MyString = "Event \"Some string data\"" ;
preg_match("/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;


JFTR: You can also use single quotes, this avoids the ugly escaping,
e.g.

preg_match('/Event "(.+)"/sU', ...);

Micha
Jul 17 '05 #3

P: n/a
Hi,

Thanks for the sample. Unfortunately I'm getting a null return value. This
looks to be a problem as my input line has square brackets in the source
string. If you could describe the components of the regular expression
string - this bit: ("/^Event \"([^\"]+)\"$/") perhaps I can add them in.

Thanks again!

"John" <jo**@nospam.org> wrote in message
news:41***********************@news.free.fr...
"Reckless" <re******@nospam.com> a écrit dans le message de news:
cj**********@titan.btinternet.com...
I've got a file with this in it:
[Event "Some string data"]

The data I'd like extracted is within the quotes: Some string data

I can read the file out and extract (using string positions) the data I'd like but it would neater if I use a regular expression. Only problem is

I've
never seen a working example of this type of extraction and am completely new to PHP.

Thanks in advance


$MyString = "Event \"Some string data\"" ;
preg_match("/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;
echo $RegArray[1] ; # should display : Some string data

--
John
www.realposition.com

Jul 17 '05 #4

P: n/a
Reckless wrote:
"John" <jo**@nospam.org> wrote in message
news:41***********************@news.free.fr...
"Reckless" <re******@nospam.com> a écrit dans le message de news:
cj**********@titan.btinternet.com...
I've got a file with this in it:
[Event "Some string data"]

The data I'd like extracted is within the quotes: Some string data

I can read the file out and extract (using string positions) the data I'dlike but it would neater if I use a regular expression. Only problem is


I've
never seen a working example of this type of extraction and am completelynew to PHP.


$MyString = "Event \"Some string data\"" ;
preg_match("/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;
echo $RegArray[1] ; # should display : Some string data

Hi,

Thanks for the sample. Unfortunately I'm getting a null return value. This
looks to be a problem as my input line has square brackets in the source
string. If you could describe the components of the regular expression
string - this bit: ("/^Event \"([^\"]+)\"$/") perhaps I can add them in.


Try something like:
preg_match('/^\[Event "(.*)(?!<("\]))"\]/sU', $str, $matches);

That would look for
[Event "

At the beginning of a line, then match everything until it finds"
"]

Then $matches[1] would have the "data"

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #5

P: n/a

"Michael Fesser" <ne*****@gmx.net> wrote in message
news:d8********************************@4ax.com...
.oO(John)
$MyString = "Event \"Some string data\"" ;
preg_match("/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;


JFTR: You can also use single quotes, this avoids the ugly escaping,
e.g.

preg_match('/Event "(.+)"/sU', ...);

Micha


That's great thanks :)

Any recommended reading on the syntax of those pattern match strings? TIA!
Jul 17 '05 #6

P: n/a
"Reckless" <re******@nospam.com> wrote in message
news:cj**********@titan.btinternet.com...
I've got a file with this in it:
[Event "Some string data"]

The data I'd like extracted is within the quotes: Some string data

I can read the file out and extract (using string positions) the data I'd
like but it would neater if I use a regular expression. Only problem is I've never seen a working example of this type of extraction and am completely
new to PHP.

Thanks in advance


$tmp = '[Event "Some string data"]';
$tmp2 = str_replace( array( '[Event "', '"]' ), '', $tmp );
Jul 17 '05 #7

P: n/a
.oO(Reckless)
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:d8********************************@4ax.com.. .
preg_match('/Event "(.+)"/sU', ...);


That's great thanks :)

Any recommended reading on the syntax of those pattern match strings? TIA!


The manual? ;)

Short explanation of the pattern above:

/ opening pattern delimiter
Event " exact string match
( start of subpattern, this will be returned as result
.. any char ...
+ ... in any number, but at least one
) close subpattern
" exact string match
/ closing delimiter
s modifier: the dot . will also match on newlines
U modifier: all quantifiers (+ for example) turn to
ungreedy and match the shortest possible result (default
is greedy with matching the longest possible result)

HTH
Micha
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.