473,799 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2251
"Reckless" <re******@nospa m.com> a écrit dans le message de news:
cj**********@ti tan.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.or g> wrote in message
news:41******** *************** @news.free.fr.. .
"Reckless" <re******@nospa m.com> a écrit dans le message de news:
cj**********@ti tan.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.or g> wrote in message
news:41******** *************** @news.free.fr.. .
"Reckless" <re******@nospa m.com> a écrit dans le message de news:
cj**********@ titan.btinterne t.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.ne t> wrote in message
news:d8******** *************** *********@4ax.c om...
.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******@nospa m.com> wrote in message
news:cj******** **@titan.btinte rnet.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.ne t> 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
10359
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 a string is a partial match to an re. Given an re of 'abcd and a bunch of other stuff' This is what i'm looking for:
1
4187
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
4
1612
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. The following code works for a single word: def createStandaloneWordRegex(word): """ return a regular expression that can find 'peter' only if it's written alone (next to space, start of string, end of string, comma, etc) but
3
2031
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 specialized code instead of an RE is almost always the better solution. Here is why.... RE's are complex. Sure it is one line of code, but it is on hell of a line. Some of my RE remind me of the obfuscated code contest winners, where one line of...
1
1602
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 value entered in a textbox matches a value I am expecting - RegEx.IsMatch(textbox.text,"^LS$"). - So this will return true if my textbox.text is "LS". What I want to do though, is to know if the value in my textbox.text does NOT match "LS".
4
5174
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 testMyColumn{1, 100}Test Basically I want the regular expression to add the word test infront of the
18
3044
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
371
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 - Album- i have try this syntax but it failed
28
16424
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 write a safe but easy implementation (i.e. no codedom) for an IBindingListView.Filter (by compiling to a Predicate<T>). Anybody know if this is possible at all? Marc
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7570
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.