tags:

views:

100

answers:

7

I'm using PHP. I'm trying to get a Regex pattern to match everything between value=" and " i.e. Line 1 Line 2,...,to Line 4.

value="Line 1
Line 2
Line 3
Line 4"

I've tried /.*?/ but it doesn't seem to work.

I'd appreciate some help.

Thanks.

P.S. I'd just like to add, in response to some comments, that all strings between the first " and last " are acceptable. I'm just trying to find a way to get everything between the very first " and very last " even when there is a " in between. I hope this makes sense. Thanks.

A: 

I'm not a regex guru, but why not just explode it?

// Say $var contains this value="..." string
$arr = explode('value="');
$mid = explode('"', $arr[1]);
$fin = $mid[0]; // Contains what you're looking for.
Josh K
I could explode but if there was " within the value=" and " I'd still have a problem. Not sure if there's a regex that can accommodate that possibility.
Chuck Ugwuh
If there is a `"` inside the string, then you need to be more precise in which strings are acceptable; in example, the string delimiter must be followed by a new line character.
kiamlaluno
@Chuck: Then stipulate that the last `"` must contain a newline after, `"\n`.
Josh K
+1  A: 

if you just want answer and not want specific regex,then you can use this:

    <?php
$str='value="Line 1
Line 2
Line 3
Line 4"';
$need=explode("\"",$str);
var_dump($need[1]);
?>
Ankur Mukherjee
+1  A: 

/.*?/ has the effect to not match the new line characters. If you want to match them too, you need to use a regular expression like /([^"]*)/.

I agree with Josh K that a regular expression is not required in this case (especially if you know there will not be any apices apart the one to delimit the string). You could adopt the solution given by him as well.

kiamlaluno
+1  A: 

If you must use regex:

if (preg_match('!"([^"]+)"!', $value, $m))
    echo $m[1];
NullUserException
A: 

Assuming the desired character is "double quote":

$pat = '/\"([^\"]*?)\"/'; // text between quotes excluding quotes
$value='"Line 1 Line 2 Line 3 Line 4"';

preg_match($pat, $value, $matches);

echo $matches[1]; // $matches[0] is string with the outer quotes
Sergei
Hi Sergei. Thanks, this works. Is there any way to just match the first quote and the very last? For example, if I have "Line 1 Line 2 Line"3 Line 4", can I modify your regex to get everything inside the first " and the very last ", including the " that appears between?
Chuck Ugwuh
yes, just modify pattern a little: /\"(.+)\"/
Sergei
+1  A: 

You need s pattern modifier. Something like: /value="(.*)"/s

racetrack
In a string `v1="value1", v2="value2"` that's going to match `"value1", v2="value2"`
NullUserException
@Null: The OP has added a comment saying that's exactly what he wants. Looks like this is the answer he's looking for.
Alan Moore
A: 

The specification isn't clear, but you can try something like this:

/value="[^"]*"/

Explanation:

  • First, value=" is matched literally
  • Then, match [^"]*, i.e. anything but ", possibly spanning multiple lines
  • Lastly, match " literally

This does not allow " to appear between the "real" quotes, not even if it's escaped by e.g. preceding with a backslash.

The […] is a character class. Something like [aeiou] matches one of any of the lowercase vowels. [^…] is a negated character class. [^aeiou] matches one of anything but the lowercase vowels.

References

Related questions

polygenelubricants
Don't you need a capture group there?
NullUserException
Thanks a lot. Is it possible to allow " between the real quotes i.e. match everything between the first " and last ", still spanning multiple lines of course?
Chuck Ugwuh
`".*"` in singleline mode (`/s` flag) will do that; look at shadowfax's answer. I'm not sure if that's really what you want, though. You can also just use `indexOf/lastIndexOf` instead of regex if the specification is really that simple.
polygenelubricants