String s1 = "abcfromCharCodedef";
String s2 = "abcString.fromCharCodedef";
String regex = ".*[^String\\.]fromCharCode.*";
System.out.println(s1.matches(regex));
System.out.println(s2.matches(regex));
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));
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));
>
> 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!
> > 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.
; )
> > :-( 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.
> > > 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
> 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.
> > 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.
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).
> 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.
> 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.