Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pattern matching find the greater than symbol using regular expression

I need a regular expression so that if I search for ">" greater than.

for example

for this string I will get true - "if x> 2"

and for this string I will get false "<template>"

I have tried this - [^<][a-zA-Z0-9_]+[a-zA-Z0-9_ ]*> as the regular expression but the problem is that it finds a substring that match for example in <template> it finds template> and return true.

thanks.

EDIT:

I am using this regular expression [^<a-zA-Z0-9_][a-zA-Z0-9_]+[ ]*> tried it over the entire firefox 1.0 source code and it seem to be working fine.

like image 273
yossi Avatar asked Feb 23 '23 21:02

yossi


1 Answers

It sounds like you want to match lines that contain > but not <. This pattern will do that:

/^(?=.*>)[^<]+$/

However, I'm curious why you want to do this. It sounds suspiciously like you're trying to parse HTML with regular expressions, which is usually A Bad Idea.

EDIT:

It's clearer now what you're trying to do, but you should be aware that this pushes the limits of what regular expressions are capable of. They can't really tell the difference between a template declaration and text with angle brackets in it, but if you know your template declarations all match a very specific pattern, you can do a pretty good job of catching them.

If all your template declarations follow the <[0-9]+template> pattern, you can do this:

/^.*(?<!<\d+template)>.*$/

If your templates don't follow such a strict convention, you need a true C++ parser for this. It will be basically impossible for regex to tell the difference between a template declaration and this:

a=b<c>d;

...which is valid code in C++ (translating, I believe, to a = (b < c) > d;).

like image 73
Justin Morgan Avatar answered Feb 26 '23 16:02

Justin Morgan