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 4 of 4

Thread: Pattern class

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Pattern class

    I read this book , Thinking in Java and I can't understand how did he managed to store this 2nd line of code (String , and regex) inside of args. Please help me. This bugs me all day. Here's program and output :


    // Allows you to easily try out regular expressions.
    // {Args: abcabcabcdefabc "abc+" "(abc)+" "(abc){2,}" }
    import java.util.regex.*;
    import static net.mindview.util.Print.*;
    public class TestRegularExpression {
    public static void main(String[] args) {
    if(args.length < 2) {
    print("Usage:\njava TestRegularExpression " +
    "characterSequence regularExpression+");
    System.exit(0);
    }
    print("Input: \"" + args[0] + "\"");
    for(String arg : args) {
    print("Regular expression: \"" + arg + "\"");
    Pattern p = Pattern.compile(arg);
    Matcher m = p.matcher(args[0]);

    while(m.find()) {

    print("Match \"" + m.group() + "\" at positions " +
    m.start() + "-" + (m.end() - 1));
    }
    }
    }

    Output:
    Input: "abcabcabcdefabc"
    Regular expression: "abcabcabcdefabc"
    Match "abcabcabcdefabc" at positions 0-14
    Regular expression: "abc+"
    Match "abc" at positions 0-2
    Match "abc" at positions 3-5
    Match "abc" at positions 6-8
    Match "abc" at positions 12-14
    Regular expression: "(abc)+"
    Match "abcabcabc" at positions 0-8
    Match "abc" at positions 12-14
    Regular expression: "(abc){2,}"
    Match "abcabcabc" at positions 0-8


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

    Default Re: Pattern class

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Be sure to properly format the code so it has indentations that shows the logic nesting levels. All statements should NOT start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Pattern class

    [code == java ]

    < // Allows you to easily try out regular expressions.
    // {Args: abcabcabcdefabc "abc+" "(abc)+" "(abc){2,}" }
    import java.util.regex.*;
    import static net.mindview.util.Print.*;
    public class TestRegularExpression {
    public static void main(String[] args) {
    if(args.length < 2) {
    print("Usage:\njava TestRegularExpression " +
    "characterSequence regularExpression+");
    System.exit(0);
    }
    print("Input: \"" + args[0] + "\"");
    for(String arg : args) {
    print("Regular expression: \"" + arg + "\"");
    Pattern p = Pattern.compile(arg);
    Matcher m = p.matcher(args[0]);

    while(m.find()) {

    print("Match \"" + m.group() + "\" at positions " +
    m.start() + "-" + (m.end() - 1));
    }
    }
    }>

    [/code]

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

    Default Re: Pattern class

    Please fix the code tags in your post. See post#2


    Be sure to properly format the code so it has indentations that shows the logic nesting levels. All statements should NOT start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. letter pattern
    By severus1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 4th, 2011, 01:24 PM
  2. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM
  3. How to Use an Observer Pattern
    By copeg in forum Java Programming Tutorials
    Replies: 0
    Last Post: February 9th, 2011, 10:24 PM
  4. Help with regex pattern
    By b_jones10634 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 24th, 2010, 03:59 PM
  5. [SOLVED] Need Regex for a pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 7
    Last Post: May 5th, 2010, 02:06 AM