I Have following data :
<h5> MY DATA </h5>
» Test data1
» Test data2
» Test data3
And I wish to match all ' »' except the first. But the various regex that i've tried do not work. Kindly advice some solution.
Thanks
But why do you want to match every » except the first one? You'll get much better responses if you tell us what you're trying to accomplish, not how you're trying to accomplish it.
As I understand it, you have a block of two or more lines that start with a certain character and you want to add a <br/> tag to the end of every line except the last one. When you describe it that way, the regex practically writes itself:
^ # beginning of line (in multiline mode)
(».+\R) # `»` and the rest of the line, including the trailing newline (`\R`)
(?=») # lookahead checks that the next line begins with `»`, too
The line is captured in group #1, so we plug it back into the replacement string and add the <br/> tag:
$result = preg_replace('/^(».+\R)(?=»)/m', '$1<br/>', $subject);
I'm not fluent in PHP, but it's possible you'll need to add the UTF8 modifier (/^(».+\R)(?=»)/mu) or use a hex escape for the » character (/^(\x{BB}.+\R)(?=\x{BB})/m).
» except the first one and not the last one :)» to the beginning of all but the first line, which is the same as adding it to the end of all but the last line. And if you think about it in those terms, it becomes much easier to see your way to a solution, or so I believe.You can try this:
$result = preg_replace('~[^>\s]\h*\R\K(?=»)~', '<br/>', $string);
details:
[^>\s] # a character that is not a white char or a > (to avoid the first line)
\h* # horizontal white chars zero or more times (possible leading spaces)
\R # a new line
\K # remove all that have been matched before
(?=») # lookahead assertion, to check if there is a » after
The goal of the pattern is to match an empty string at the good position in the string.
/(^[\rn]*|[\r\n]+)[\s\t]*[\r\n]+/
But the various regex that i've tried do not workwell then show us what you have tried and why it did fail. Also please be precise, what if there was another random line likefoobarbetween» Test data1and» Test data2. Would it be matched or not ?