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

regexp prob

this is driving my crazy:

$set = '<Error Type="string" Value="System Error" /><ErrorCode
Type="int" Value="0" />';
preg_match('%<Error .*? Value="(.*)"%', $set, $matches);
echo $matches[1];
returns: System Error" />

why on earth does it include the " /> part when my pattern clearly asks
only to capture what's between the double quotes with "(.*)" ?
Jul 17 '05 #1
3 1502
Filip <fi************@mrbookmaker-nv.no.spam.be> wrote:
this is driving my crazy:

$set = '<Error Type="string" Value="System Error" /><ErrorCode
Type="int" Value="0" />';
preg_match('%<Error .*? Value="(.*)"%', $set, $matches);
echo $matches[1];
returns: System Error" />

why on earth does it include the " /> part when my pattern clearly asks
only to capture what's between the double quotes with "(.*)" ?


Because .* without trailing question mark will consume as many characters
as possible. In your example that would be:
<Error Type="string" Value="System Error" /><ErrorCode Type="int" Value="0" />';
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #2
Simon Stienen wrote:
Filip <fi************@mrbookmaker-nv.no.spam.be> wrote:
this is driving my crazy:

$set = '<Error Type="string" Value="System Error" /><ErrorCode
Type="int" Value="0" />';
preg_match('%<Error .*? Value="(.*)"%', $set, $matches);
echo $matches[1];
returns: System Error" />

why on earth does it include the " /> part when my pattern clearly asks
only to capture what's between the double quotes with "(.*)" ?

Because .* without trailing question mark will consume as many characters
as possible. In your example that would be:
<Error Type="string" Value="System Error" /><ErrorCode Type="int" Value="0" />';
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ah I was sure that wasn't the problem because it only showed " />, but I
guess anything starting with < is not shown in the browser. In any case,
adding the ? made it work, thanx!
Jul 17 '05 #3
.oO(Filip)
ah I was sure that wasn't the problem because it only showed " />, but I
guess anything starting with < is not shown in the browser.
While debugging use

htmlspecialchars(echo $matches[1])

instead, if the result contains HTML-code. Or use

header('Content-type: text/plain');

somewhere before.
In any case,
adding the ? made it work, thanx!


If you have more quantifiers in your pattern you could also use the
modifier U which turns all quantifiers to ungreedy.

Micha
Jul 17 '05 #4

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

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
0
by: Chris Croughton | last post by:
I'm trying to use the EXSLT regexp package from http://www.exslt.org/regexp/functions/match/index.html (specifically the match function) with the libxml xltproc (which supports EXSLT), but...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
6
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.