Regular Expression to match only one angle bracket

regex match between parentheses
regular expression angle bracket
regex match brackets
regular expression brackets
java regex angle brackets
regex between brackets
regular expression examples
java regex tester

I'm looking for a regular expression that matches the '>' in

a > b
>
b>
...

but not two or more angled brackets, i.e. it should not match

a>>b
>>
b>>
...

I was sure to do that with lookaheads or lookbehinds, but for some reason neither

\>(?!\>)

nor

(?<!\>)\>

work..?

Thanks!

Perl syntax:

/(?<!>)>(?!>)/

Without using lookahead or lookbehind:

/(?:^|[^>])>(?:[^>]|$)/

Supercharged Python: Take Your Code to the Next Level, Allow > in attribute values Like the quick and dirty regex we've just described, this next one is included primarily to contrast As before, the regex uses literal angle bracket characters at the edges of the regex to match the start and end of a tag  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Are the angle brackets (< or >) special in a regular expression?

perreal's first regex is correct. However, the second regex given in that answer subtly fails in one condition. Since it captures characters both before and after, two >s separated by a single character will not both be found.

Here is a regex which only uses forward lookaheads and doesn't have that problem:

(?:^|[^>])(>)(?:$|(?!>))

Edit live on Debuggex

Regular Expressions Cookbook: Detailed Solutions in Eight , Greedy and lazy are simply terms to describe how certain regular expression modifiers behave. + modifier tells the regular expression engine to match the preceding character one or more times. When the regular expression engine encounters the first angle bracket, which Lazy Modifiers Lazy modifiers work in exactly. POSIX bracket expressions match one character out of a set of characters, just like regular character classes. They use the same syntax with square brackets. A hyphen creates a range, and a caret at the start negates the bracket expression. One key syntactic difference is that the backslash is NOT a metacharacter in a POSIX bracket expression.

The issue here is that when you use the lookahead, you're matching the second > (there's no > after the second >), and when you're using the lookbehind, you're matching the first >.

You could probably use this:

[^>]>[^>]

There's a >, with no > before or after it.

But I think that to suit what you exactly need, need to use lookaheads and lookbehinds both:

(?<!>)>(?!>)

Securing PHP Web Applications, However, transposed to the angle brackets, this expression doesn't work: \<(.*?)\< Nor does this one: \\<(. I'm editing a TMX file in CafeTran and I'm looking for the Java regular expression that matches all characters between angle brackets Is this a CafeTran quirk?--just had to repost this because of the fresh desk editor  All HTML markup begins with a left angle bracket (<) and ends with a right angle bracket (>), and has markup information in between. Before diving in and trying to remove the markup, let's make sure we can find it correctly. An obvious (but ill-advised) choice for the regular expression is <.*>. In words it means "a literal left angle bracket, followed by one or more of any character, followed by a literal right angle bracket".

REGEX Java regular expression all characters between angle , Regular Expressions are sets of characters and/or metacharacters that match An RE matches a single character or a set of characters -- a string or a part of a string. The angle brackets must be escaped, since otherwise they have only their  For some reason, people often seem to confuse and [] in regular expressions. Say you want to match only the strings "bar" and "car". I sometimes see this mistake: /[b|c]ar/ It will indeed match "bar" and "car" as intended. But it will also match "|ar". Round brackets do grouping (and capture groups, and some other things).

A Brief Introduction to Regular Expressions, Basic and extended regular expressions differ only in the metacharacters A bracket expression enclosed in square brackets is a regular expression that matches a Within a character class expression (one made with square brackets​), the  A character class is a list of character options; one and only one of the characters in the bracketed class must appear for a match. A - (hyphen) in between two characters enclosed by brackets designates a range; for example [a-z] is the character range of the lower case twenty-six letters of the alphabet.

regexp -- syntax of regular expression patterns, Regular Expressions are sets of characters and/or metacharacters that match An RE matches a single character or a set of characters -- a string or a part of a string. The angle brackets must be escaped, since otherwise they have only their  Matches the left angle bracket in "<mno" and assigns it to the Open group. Its Group.Captures collection now has a single value, "<". 11 [^<>]* Matches "mno". 12)+ "<mno" is the value of the second captured group. The next character in the input string is an left angle bracket, so the regular expression engine loops back to the (?'Open'<)[^<>]*) subpattern. 13

Comments
  • You'll need to be more specific about the kinds of "regular" expressions you mean, as this doesn't sound like an actual regular language to me.
  • It would be cleaner to write (?:^|[^>])(>)(?=$|[^>]), IMO.
  • There is no reason to escape >. It is not a special regex character.
  • @Treecj Could you perhaps mark an answer as accepted to indicate that your problem had been solved? ^^