Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Please match this

  1. #1
    Junior Member
    Join Date
    Feb 2021
    Location
    Colorado Springs
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please match this

    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

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class equalEquals
    {
    /**
    * Constructor for objects of class equalEquals
    */
    public static void main(String arg[])
    {
    String line = "-rw-rw-r-- 1 sherman sherman 0 Feb 14 12:17 nonpedalSwing";

    // Create a Pattern object
    String pattern = "^(\\w+)(\\s)(\\d+)(\\s)(\\w+)";
    Pattern r = Pattern.compile(pattern);

    // Create a Matcher object
    Matcher m = r.matcher(line);
    if (m.find()) {
    System.out.println("Found value: \"" + m.group(0) + "\"");
    System.out.println("Found value: \"" + m.group(1) + "\"");
    System.out.println("Found value: \"" + m.group(2) + "\"");
    System.out.println("Found value: \"" + m.group(3) + "\"");
    // System.out.println("Found value: \"" + m.group(4) + "\"");
    } else {
    System.out.println("No match");
    }

    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please match this

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2021
    Location
    Colorado Springs
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please match this

    Please accept my apologies. I didn't know about cross posting until code ranch posted the 7 years ago cross posting post. How do I now correct this?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please match this

    Start by documenting the regex symbols that you are using in the pattern. For example:
            // Create a Pattern object
            // ^	The beginning of a line
            // () denotes a capturing group.
    Finish the list for the pattern you have coded.
    Then compare the patterns against the actual String being matched.


    What is the desired printout?

    So far this is what I get:
    Found value: "-rw-rw-r-- 1 sherman"
    Found value: "-rw-rw-r--"
    Found value: " "
    Found value: "1"
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2021
    Location
    Colorado Springs
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please match this

    Thank you to all now how do I close and mark this as solved? Sherman

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please match this

    Can you post your solution?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Match Mobile carrier
    By alfredpo91 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2014, 12:21 PM
  2. quick match
    By vineet kumar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 5th, 2013, 02:06 AM
  3. Java equivalant of Python re.match()
    By gonzalioz in forum Java Theory & Questions
    Replies: 1
    Last Post: October 30th, 2011, 10:25 AM

Tags for this Thread