views:

100

answers:

4

I've been struggling with getting a working regular expression for a little project of mine. Can anyone help me with a regex that matches anything inside <> symbols, but only when they are not preceded by a \ symbol?

For example:

<Escaped characters \<\> are right in the middle of this sentence.>, <Here is another sentence.>

Must match

1: Square brackets \<\> are right in the middle of this sentence.
2: here is another sentence.

So far I've managed

/<([^\\][^>]*?)>/ig

but that gives

1: Escaped characters \<\
2: Here is another sentence.

What am I doing wrong? :(

A: 

What you need are look-behind operators. Read about them here:

http://www.perl.com/pub/a/2003/07/01/regexps.html

And here is the expression you need:

/<(?!<\\).*>(?!<\\)/

As the * operator above is greedy, it should include any escaped angle brackets /< />

EDIT: I am assuming that you want escaped angle brackets to be matched and returned. If you want something different, please clarify - give a succinct example of a) the input string and b) the match to be returned

Crimson
Thanks for the reply. I'm after the content inside each set of angle brackets.Some example input: "<example>, <this is text>, <Oh god \<\> there are brackets in this one>".This will output three different strings which I will access with backreferences. These are "example", "this is text" and "Oh god \<\> there are brackets in this one".
Monte
A: 

Try this

/<[^\\]([^>]+)>/
RaYell
This is not checking for escaped angle brackets which need to be included.
Crimson
+1  A: 
beggs
That's it! Brilliant sir, thank you very much.
Monte
Doesn’t work for `<foo\>bar>`.
Gumbo
@Gumbo: works for me, see edit.
beggs
+1  A: 

I would use this:

/<((?:[^\\>]+|\\.)*)>/
Gumbo
Works nicely. Less probes than mine.
beggs