regex

Hi,The regex that I want, must check on the presence of the string "fromCharCode", but not when it is written like this "String.fromCharCode".Is this possible with regex?
[191 byte] By [Floppya] at [2007-11-26 21:17:09]
# 1
> Is this possible with regex?Yes :)
kajbja at 2007-7-10 2:55:55 > top of Java-index,Java Essentials,Java Programming...
# 2

String s1 = "abcfromCharCodedef";

String s2 = "abcString.fromCharCodedef";

String regex = ".*[^String\\.]fromCharCode.*";

System.out.println(s1.matches(regex));

System.out.println(s2.matches(regex));

prometheuzza at 2007-7-10 2:55:55 > top of Java-index,Java Essentials,Java Programming...
# 3
> String regex = ".*[^String\\.]fromCharCode.*";Is that expression really correct? I would use negative look behind.Kaj
kajbja at 2007-7-10 2:55:55 > top of Java-index,Java Essentials,Java Programming...
# 4

You need to say how you are going to use this regex because this meets your specification but I don't think it is what you want.String regex = "(?!String\\.)fromCharCode";

System.out.println("String.fromCharCode".matches(regex));

System.out.println("fromCharCode".matches(regex));

sabre150a at 2007-7-10 2:55:56 > top of Java-index,Java Essentials,Java Programming...
# 5

I'll post my expression as well:

String test1 = "some text and fromCharCode in it";

String test2 = "some text and String.fromCharCode in it";

String regexp = ".*?(?<!String\\.)fromCharCode.*?";

System.out.println(test1.matches(regexp));

System.out.println(test2.matches(regexp));

>

kajbja at 2007-7-10 2:55:56 > top of Java-index,Java Essentials,Java Programming...
# 6

> You need to say how you are going to use this regex

> because this meets your specification but I don't

> think it is what you want.> String regex = "(?!String\\.)fromCharCode";

> System.out.println("String.fromCharCode".matches(regex

> ));

>

> ystem.out.println("fromCharCode".matches(regex));

>

:-( Ignore this! It is rubbish!

sabre150a at 2007-7-10 2:55:56 > top of Java-index,Java Essentials,Java Programming...
# 7

> > String regex = ".*[^String\\.]fromCharCode.*";

>

> Is that expression really correct? I would use

> negative look behind.

>

> Kaj

I quickly tried my solution (once) and it worked. However, my knowledge of the dark regex-force is very limited*, so you are probably right.

* I even have to look up what you mean by negative look behind.

; )

prometheuzza at 2007-7-10 2:55:56 > top of Java-index,Java Essentials,Java Programming...
# 8
> :-( Ignore this! It is rubbish!In what way?
kajbja at 2007-7-10 2:55:56 > top of Java-index,Java Essentials,Java Programming...
# 9

> > :-( Ignore this! It is rubbish!

>

> In what way?

I meant to use negative look behind but typed the negative look ahead! My regex is rubbish because it failsSystem.out.println("String.xzzzzzfromCharCode".matches(regex));

which should pass.

sabre150a at 2007-7-10 2:55:56 > top of Java-index,Java Essentials,Java Programming...
# 10

> > > String regex = ".*[^String\\.]fromCharCode.*";

> >

> > Is that expression really correct? I would use

> > negative look behind.

> >

> > Kaj

>

> I quickly tried my solution (once) and it worked.

> However, my knowledge of the dark regex-force is very

> limited*, so you are probably right.

I don't know much either about regexp, but I'm learning :)

One of the problems of your regexp is [^String\\.] that doesn't do what you think it does (if I'm correct). It creates a character class which will match any character except for S t r i n g \ and .

(And it will not care about the order of the characters)

Kaj

kajbja at 2007-7-10 2:55:56 > top of Java-index,Java Essentials,Java Programming...
# 11
> I meant to use negative look behind but typed the> negative look ahead! I got surprised when I saw that your example worked, and was about to ask why it worked, but now I understand :)
kajbja at 2007-7-10 2:55:56 > top of Java-index,Java Essentials,Java Programming...
# 12

> I don't know much either about regexp, but I'm

> learning :)

Me too. I really have to read a decent regex book soon!

> One of the problems of your regexp is [^String\\.]

> that doesn't do what you think it does (if I'm

> correct). It creates a character class which will

> match any character except for S t r i n g \ and .

> (And it will not care about the order of the

> characters)

>

> Kaj

Ah, I understand. Thanks Kaj.

prometheuzza at 2007-7-10 2:55:56 > top of Java-index,Java Essentials,Java Programming...
# 13
> Me too. I really have to read a decent regex book> soon!I have just bought "Mastering Regular Expressions" but I haven had time to read it :(My plan is to beat uncle alice around year 2037 :)
kajbja at 2007-7-10 2:55:57 > top of Java-index,Java Essentials,Java Programming...
# 14
> My plan is to beat uncle alice around year 2037 :)I don't know how old uncle_alice is, but if he's currently in his 90's, I give you a decent chance to beat him by then!; )
prometheuzza at 2007-7-10 2:55:57 > top of Java-index,Java Essentials,Java Programming...
# 15

> > My plan is to beat uncle alice around year 2037 :)

>

> I don't know how old uncle_alice is, but if he's

> currently in his 90's, I give you a decent chance to

> beat him by then!

> ; )

By then he will likely have a regex that regex's for him - perhaps in a recisive way.

Aknibbsa at 2007-7-21 18:15:16 > top of Java-index,Java Essentials,Java Programming...
# 16

I plan to continue my regex studies after I'm dead, eventually to achieve ultimate, blissful mastery. But don't let that discourage you; rather, my hope is to inspire others to strive for excellence, for my name is really Jonathan Livingston Hacker.

BTW, the backslashes in "[^String\\.]" won't match a literal backslash. They just escape the dot, which doesn't need to be escaped when it appears in a character class. Inside a character class or out, you have to use four backslashes to match one (when you're writing the regex in the form of a String literal, that is).

uncle_alicea at 2007-7-21 18:15:16 > top of Java-index,Java Essentials,Java Programming...
# 17
uncle_alice how did you get so good at regex's ? Is that a main portion of your coding or something ?
Aknibbsa at 2007-7-21 18:15:16 > top of Java-index,Java Essentials,Java Programming...
# 18

> I plan to continue my regex studies after I'm dead,

> eventually to achieve ultimate, blissful mastery.

> But don't let that discourage you; rather, my hope

> is to inspire others to strive for excellence, for

> my name is really Jonathan Livingston Hacker.

Uncle_alice will live on as a self-aware, self-documenting, artificially intelligent Perl script with aspirations of world domination. He will rewrite and recompile himself to native code on demand.

kevjavaa at 2007-7-21 18:15:16 > top of Java-index,Java Essentials,Java Programming...
# 19

> uncle_alice how did you get so good at regex's ?

> Is that a main portion of your coding or something ?

Other way 'round: I use them a lot because I'm good at them. I got hooked on them when I was doing a lot of Perl programming a while back. Regexes are a fundamental part of the Perl culture, which means there are lots of good tutorials and reference docs available online, lots of examples of regex usage in existing code, and lots of regex experts who can answer questions about them.

And, of course, there's [url=http://www.amazon.com/exec/obidos/ASIN/0596528124/masteringregu-20]The Book[/url]. If you really want to get good at regexes, read that book; no other book comes close. It may take more than one reading for the concepts to really sink in; it did for me.

I've also learned a lot by hanging out in these forums, answering other people's questions.

uncle_alicea at 2007-7-21 18:15:16 > top of Java-index,Java Essentials,Java Programming...