\" greater than. for example for this string I will get true - \"开发者_C百科if x> 2\"" />
开发者

pattern matching find the greater than symbol using regular expression

开发者 https://www.devze.com 2023-03-13 10:42 出处:网络
I need a regular expression so that if I search for \">\" greater than. for example for this string I will get true - \"开发者_C百科if x> 2\"

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

for example

for this string I will get true - "开发者_C百科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.


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;).


A regex seems like the wrong tool for the job you're trying to do. You'll probably require a full-blown C++ parser to reliably distinguish ">" the greater-than operator from ">" the template delimiter, or ">" as part of a string literal or comment.

0

精彩评论

暂无评论...
验证码 换一张
取 消