1

Is it possible to create a single regexp to replace < and > with their entity equivalents in Komodo Edit?

s/<|>/&lt;|&gt;/
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265

6 Answers6

2

I'm guessing that you may have to convert & to &amp; and so on.

If this is the case there's most likely a library or function in whichever language/platform you're using (e.g. in Java check out StringEscapeUtils). Indicate which language you're using and someone here will no doubt point you to something appropriate.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

It is easy in to do this in just about any language without using regex:

PHP:

$xml = str_replace(array('>', '<'), array('&gt;','&lt;'), $xml);

Python:

xml = xml.replace('>', '&gt;').replace('<','&lt;');

etc.

too much php
  • 88,666
  • 34
  • 128
  • 138
0

It depends on the language you are using. In Perl, you could do:

s/([<>])/$1 eq '<' ? '&lt;' : '&gt;'/ge

Other languages usually allow you to provide a match callback function that returns a replacement string. To wit: In C#, you can do this:

Regex.Replace("<", "([<>])", x => x.Value == "<" ? "&lt;" : "&gt;")
brianary
  • 8,996
  • 2
  • 35
  • 29
0

You could use a hash-variable, something like:

my %data;
$data{"<"} = '&lt;';
$data{">"} = '&gt;';
s/(<|>)/$data{$1}/g;
Johan Soderberg
  • 2,650
  • 1
  • 15
  • 12
0

Thanks everyone. I was looking for something I could use in Komodo Edit, so variables and conditional statements were not an option. Here is the best solution I found, which was based on a Sed tutorial at IBM Developerworks:

s/<([^>]*)>([^<]*)<([^>]*)>/&lt;\1&gt;\2&lt;\3&gt;/
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
0

In Komodo Edit 5.x, use the moreKomodo extension to save the following find/replace regex search:

Find:

<([^>]*)>([^<]*)<([^>]*)>

Replace:

&lt;\1&gt;\2&lt;\3&gt;
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265