473,416 Members | 1,801 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,416 software developers and data experts.

Regular expression help please (hopefully simple)

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
7 2236
"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
.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
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
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

"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
"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
.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ron Adam | last post by:
Is it possible to match a string to regular expression pattern instead of the other way around? For example, instead of finding a match within a string, I want to find out, (pass or fail), if...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own....
3
by: Tom | last post by:
I have struggled with the issue of whether or not to use Regular Expressions for a long time now, and after implementing many text manipulating solutions both ways, I've found that writing...
1
by: Kate Bieren | last post by:
This should be easy but I am brand new to regular expressions so hopefully someone can help me. I want to do some simple field level validation in my app and I can do a simple test to see if the...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.