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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
deepsoft
Forum Newbie
Posts: 1
Joined: Wed Feb 07, 2007 1:41 pm

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

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

preg_split on something like "<[A-Z]+>" with the PREG_SPLIT_DELIM_CAPTURE flag set.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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.
Post Reply