Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Please match this

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying unsuccessfully to use Java to match a line so I can use particular parts of the line. The String line is:
String line = "-rw-rw-r-- 1 sherman sherman 0 Feb 14 12:17 nonpedalSwing";
I am using Pattern and Matcher. This is an example of how I want to seperate the line so I can use sub 2, sub 3, and sub 5.
String Pattern = "^(-rw-rw-r-- 1 sherman sherman 0)(Feb)(14)( 12:17 )(nonpedalSwing)";
This does not work for me:
String Pattern = "^(-\\w+-\\w+-\\w--\\s\\d\\s\\w+\\s\\w+\\s+\\d\\s)(\\w+)(\\s)(\\d+)(\ \s\\d+:\\d+\\s)(\\w+)";
At the end of this message is the code I am playing with.
How would you do it?

Thanks; Sherman
 
Master Rancher
Posts: 4176
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted here:  https://www.javaprogrammingforums.com/whats-wrong-my-code/43543-please-match.html#post170752

Please read:  https://coderanch.com/wiki/660346/Wiki/Forthright-Cross-Posting-Sites
 
Sherman Willden
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please accept my apologies. I didn't know that cross-posting was such a bad thing. How do I now get an answer to my question?

Thanks for sharing the 7-year old cross posting post.

Sherman
 
Marshal
Posts: 3398
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would need to remove these 2 characters from the pattern to get a match with the sample data that you included:

And you probably want to remove these brackets so you don't have a capture group containing a single space:

Also, you probably want these spaces to be outside of your bracketed capture groups:

 
Ron McLeod
Marshal
Posts: 3398
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also this part of the pattern which is used to match the file permissions seems a bit fragile:
For example: if the permissions changed from -rw-rw-r-- to -r--r--r-- your pattern would not match.  A safer pattern might be something like:or
 
Bartender
Posts: 2823
149
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my suggestion:
Explanation:
Assumption: the dates and times are in 2 digit formats strictly.
Let us know if it works
 
Bartender
Posts: 7730
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My $.02 - in the beginning...

or

 
lowercase baba
Posts: 12962
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
question:
Is there a reason for doing it this way?  Couldn't you use something like String.split() on whitespace characters, then check the specific array elements for the values?
 
Norm Radder
Master Rancher
Posts: 4176
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

use particular parts of the line.


Does the OP need to define what those parts are before any code to parse them out can be written?
 
Sherman Willden
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you to everone and I really appreciate the time you all took to answer this. I have been using PERL regex since 1993 so I thought this would be an easy step to develop this in Java. Came as a surprise when I couldn't. I am retired and working at the Home Depot and using my off time to improve myself. Again thanks to all. Now how do I mark this as solved and closed?

Sherman
 
Norm Radder
Master Rancher
Posts: 4176
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your solution?
 
Sherman Willden
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Norm. I will but I am working today and tomorrow so it will be Sunday. I appreciate everthing everyone has posted.
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic