1

I want to restrict usage of unescaped ampersands in a particular input field. I'm having trouble getting a RegEx to kill usage of "&" unless followed by "amp;"...or perhaps just restrict usage of "& " (note the space).

I tried to adapt the answer in this thread, but to no avail. Thanks.

(FWIW, here's a RegEx I made to ensure that a filename field didn't contain restrited chars. and ended in .mp3. It works fine, but does it look efficient?)

^[^&,<,>,:,",/,\\,|,?,\*]+(\.mp3|\.MP3|\.Mp3|\.mP3)$
Community
  • 1
  • 1
JDV72
  • 29
  • 1
  • 5
  • Where's the link to your regex? – John Jan 31 '11 at 23:15
  • do you also want to allow e.g. the german a umlaut ä? Or other international characters? – Sören Jan 31 '11 at 23:18
  • @John - Apprently it got stripped out without me noticing....will try again. @Soren - Those chars. won't be a problem for me, but feel free to show how for others who may see this question later. – JDV72 Feb 03 '11 at 20:03

2 Answers2

8

This regular expression matches any occurrence of & which is not followed by amp;:

/&(?!amp;)/

Rubular

This regular expression accepts strings that contain characters except &, or the string &amp;:

/^([^&]|&amp;)*$/

Rubular

You can use either one or the other, depending on which is most convenient. The difference is that the string should be rejected if the first regular expression matches, whereas the string should be accepted if the second regular expression matches.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

You can match on /&(?!amp;)/ to locate any &'s not followed by &amp. The (?!) construction is called a negative lookahead.

Assuming you're using a regexp engine that supports them, at any rate. I know Perl/PCRE regexps do.

Twisol
  • 2,762
  • 1
  • 17
  • 17