Page 1 of 1

Regex to capture anything but <[A-Z]*>

Posted: Wed Feb 07, 2007 1:57 pm
by deepsoft
Hi,


I am trying to write a regex to capture anything but < followed by upper case letters followed by >. For example

<YEAR>-<MONTH>-<DAY> => I want to capture - and replace it with ' "-" ' so the output string should be <YEAR> "-" <MONTH> "-" <DAY>

<YEAR>.<MONTH>.<DAY>. => output should be <YEAR> "." <MONTH> "." <DAY> "."


I have tried the following


[^<][^A-Z]*[^>]

[^<[^A-Z]*^>]

(^(<[A-Z]*>)

I am not able to figure out how exactly I will be able to capture and replace it. I can do it in otherway by finding indexes of string literals and then doing math but that is not a clean way to do this.

Any help to write this will be appreciated.

Thank you.

Posted: Wed Feb 07, 2007 2:17 pm
by Mordred
preg_split on something like "<[A-Z]+>" with the PREG_SPLIT_DELIM_CAPTURE flag set.

Posted: Thu Feb 08, 2007 7:14 am
by dude81
$string="I assume your content string here";

Code: Select all

$pattern ="/(<.[^A-Z]+>)/s";
$content=preg_split($pattern, $string, -1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
Not sure of this since I dont have data to test, but I hope a bit of permutations can possibly make a solution.
Let us know if this helped.