About Question enthuware.ocpjp.v7.2.1211 :

Moderator: admin

Post Reply
thchuong
Posts: 8
Joined: Wed Sep 10, 2014 2:42 am
Contact:

About Question enthuware.ocpjp.v7.2.1211 :

Post by thchuong » Thu Jan 22, 2015 9:55 pm

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

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Jan 23, 2015 10:23 pm

You are not trying to escape *. You are trying to escape two backslashes i.e. \\.
If you like our products and services, please help us by posting your review here.

thchuong
Posts: 8
Joined: Wed Sep 10, 2014 2:42 am
Contact:

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

Post by thchuong » Sun Jan 25, 2015 9:14 pm

Thank you for your answer.

OK now why we need two backslashes in this case?

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Jan 25, 2015 9:30 pm

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
If you like our products and services, please help us by posting your review here.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

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

Post by EelcoD » Mon Aug 10, 2015 2:18 am

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?

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon Aug 10, 2015 2:35 am

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.
If you like our products and services, please help us by posting your review here.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

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

Post by EelcoD » Mon Aug 10, 2015 2:42 am

But a.java resides in the pathtest directory, so you're not crossing any boundaries.
What am I missing?

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon Aug 10, 2015 3:11 am

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.
If you like our products and services, please help us by posting your review here.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

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

Post by EelcoD » Mon Aug 10, 2015 3:18 am

So, the *.java will try to match it with c:\ or with works\ ?

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon Aug 10, 2015 4:03 am

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.)
If you like our products and services, please help us by posting your review here.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

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

Post by EelcoD » Mon Aug 10, 2015 4:11 am

It's hard for me to explain aparently..

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

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon Aug 10, 2015 4:44 am

I guess you can say that.
If you like our products and services, please help us by posting your review here.

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

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

Post by codecodecode67 » Fri May 20, 2016 3:51 pm

Would "glob:*{.java}" and "glob:*.{java}" match all the java files just as "glob:**.java" would?

Thanks

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri May 20, 2016 9:46 pm

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

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

Post by codecodecode67 » Sat May 21, 2016 7:04 am

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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests