2

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.

ObiHill
  • 11,448
  • 20
  • 86
  • 135

7 Answers7

4

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
  • 2,747
  • 1
  • 17
  • 16
  • 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? – ObiHill Aug 07 '10 at 16:15
1

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
  • 3,777
  • 5
  • 32
  • 39
1

/.*?/ 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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
1

If you must use regex:

if (preg_match('!"([^"]+)"!', $value, $m))
    echo $m[1];
NullUserException
  • 83,810
  • 28
  • 209
  • 234
1

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

racetrack
  • 3,766
  • 30
  • 30
0

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
  • 28,364
  • 20
  • 86
  • 132
  • 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. – ObiHill Aug 07 '10 at 05:31
  • 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. – apaderno Aug 07 '10 at 05:40
  • @Chuck: Then stipulate that the last `"` must contain a newline after, `"\n`. – Josh K Aug 07 '10 at 06:10
0

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

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • Don't you need a capture group there? – NullUserException Aug 07 '10 at 11:56
  • 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? – ObiHill Aug 07 '10 at 16:18
  • `".*"` 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 Aug 07 '10 at 16:39