Page 1 of 1

About Question enthuware.ocpjp.v7.2.1211 :

Posted: Thu Jan 22, 2015 9:55 pm
by thchuong
Hi,

In this link of java doc http://docs.oracle.com/javase/7/docs/ap ... .String%29
I don't understand why "*" has to be escaped in this line:
C:\\* Matches C:\foo and C:\bar on the Windows platform (note that the backslash is escaped; as a string literal in the Java Language the pattern would be "C:\\\\*")

why shouldn't it be C:\* and in java string "C:\\*"?

Thank you

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Fri Jan 23, 2015 10:23 pm
by admin
You are not trying to escape *. You are trying to escape two backslashes i.e. \\.

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Sun Jan 25, 2015 9:14 pm
by thchuong
Thank you for your answer.

OK now why we need two backslashes in this case?

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Sun Jan 25, 2015 9:30 pm
by admin
Because you are trying to match c:\foo not c:/foo. So you need to escape \ in the path matcher expression itself. So you get two \ in the expression. When you put it in java code, you need to escape each of them.
This explains more: http://www.jedit.org/users-guide/globs.html
http://mywiki.wooledge.org/glob

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Mon Aug 10, 2015 2:18 am
by EelcoD
Why isn't this discussion about the related question?

I don't understand the explanation: "Path objects that we are checking contain the complete path including directories (such as c:\works\pathtest\a.java and not just a.java). Therefore, nothing will match."

Why doesn't it match a.java
*.java would match everything that ends with .java, right?

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Mon Aug 10, 2015 2:35 am
by admin
EelcoD wrote:Why isn't this discussion about the related question?
What do you mean? The question is about matching paths.
I don't understand the explanation: "Path objects that we are checking contain the complete path including directories (such as c:\works\pathtest\a.java and not just a.java). Therefore, nothing will match."

Why doesn't it match a.java
*.java would match everything that ends with .java, right?
Wrong. The * character matches zero or more characters of a name component without crossing directory boundaries.
Please go through this for complete details : http://docs.oracle.com/javase/7/docs/ap ... ng.String)

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Mon Aug 10, 2015 2:42 am
by EelcoD
But a.java resides in the pathtest directory, so you're not crossing any boundaries.
What am I missing?

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Mon Aug 10, 2015 3:11 am
by admin
If you run the program and print each Path given to MyFileChecker by walkFileTree, you will see that the Paths are not relative paths. They all start with c:\works\pathtest. So it doesn't get a.java. It gets c:\works\pathtest\a.java. That is why the match fails.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Mon Aug 10, 2015 3:18 am
by EelcoD
So, the *.java will try to match it with c:\ or with works\ ?

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Mon Aug 10, 2015 4:03 am
by admin
EelcoD wrote:So, the *.java will try to match it with c:\ or with works\ ?
Not sure what you mean. I would suggest you to run the program and print the value of Path in the check method. Basically, *.java doesn't match c:\works\pathtest\a.java. *.java will only match a.java (which you are not getting in the check method.)

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Mon Aug 10, 2015 4:11 am
by EelcoD
It's hard for me to explain aparently..

The * doesnt cross boundaries meaning it doesnt check past the first backslash?

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Mon Aug 10, 2015 4:44 am
by admin
I guess you can say that.

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Fri May 20, 2016 3:51 pm
by codecodecode67
Would "glob:*{.java}" and "glob:*.{java}" match all the java files just as "glob:**.java" would?

Thanks

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Fri May 20, 2016 9:46 pm
by admin
What happened when you tried it out?

Re: About Question enthuware.ocpjp.v7.2.1211 :

Posted: Sat May 21, 2016 7:04 am
by codecodecode67
yeah, they don't...we have to always match against the name of the file, not the whole path if we want one asterisk to work