Results 1 to 13 of 13
  1. #1
    sidharth is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default Java Regular expression ?

    Hi All,

    i want to split the following paragraph using some condition.
    the condition is , get blocks which starts with Name and ends with ! .
    How to achieve this using regex .here the Name must be at the starting of a line ignore other Name strings.

    Paragraph :

    String myInput = "Name hhhhh class0"+ "\n"+
    "HHHHHHHHHHHHHHHHHH"+ "\n"+
    "!"+ "\n"+
    "Name TTTTT TTTT"+ "\n"+
    "GGGGGG UUUUU IIII"+ "\n"+
    "!"+ "\n"+
    "Name JJJJJ WWWW"+ "\n"+
    "IIIIIIIIIIIIIIIIIIIII"+ "\n"+
    "!"+ "\n"+
    "RRRRRRRRRRR TTTTTTTT"+ "\n"+
    "HHHHHH"+ "\n"+
    "JJJJJ 1 Name class1"+ "\n"+
    "LLLLL 5 Name class5"+ "\n"+
    "!"+ "\n"+
    "OOOOOO HHHH FFFFFF"+ "\n"+
    "service 0 Name class12"+ "\n"+
    "!"+ " \n"+
    "JJJJJ YYYYYY 3/0"+ "\n"+
    "KKKKKKK"+ "\n"+
    "UUU UUU UUUUU"+ "\n"+
    "QQQQQQQ"+ "\n"+
    "!";

    My output should be :

    Name hhhhh class0
    HHHHHHHHHHHHHHHHHH
    !

    Name TTTTT TTTT
    GGGGGG UUUUU IIII
    !

    Name JJJJJ WWWW
    IIIIIIIIIIIIIIIIIIIII
    !

    Please anyone help me ....

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What have you tried?

  3. #3
    sidharth is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    hi r035198x,
    i am new to java.regex . so i havn't tried ..please help me ..

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Then get a regex tutorial, read it and make an attempt and post if you get problems with your attempt.
    Last edited by r035198x; 11-10-2009 at 03:26 PM. Reason: added "read it"

  5. #5
    sidharth is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    plz suggest me if u find any good basic tutorial for java regex ..

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

  7. #7
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    You mention you are new to Java regex, have you ever worked with regular expressions in a different language? If you have used regular expressions before (in any language), then Java regex won't be much different.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  8. #8
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    public class sort_pgrgraph
    {String myInput; char newline; char ecxl; StringBuffer input_taken;

    sort_ppgraph(){myInput = new String ("Name hhhhh class"+ "\n"+"HHHHHHHHHHHHHHHHHH"+ "\n"+"!"+ "\n"+"Name TTTTT TTTT"+ "\n"+"GGGGGG UUUUU IIII"+ "\n"+"!"+ "\n"+"Name JJJJJ WWWW"+ "\n"+"IIIIIIIIIIIIIIIIIIIII"+ "\n"+"!"+ "\n"+"RRRRRRRRRRR TTTTTTTT"+ "\n"+"HHHHHH"+ "\n"+"JJJJJ 1 Name class1"+ "\n"+"LLLLL 5 Name class5"+ "\n"+"!"+ "\n"+"OOOOOO HHHH FFFFFF"+ "\n"+"service 0 Name class12"+ "\n"+"!"+ "\n"+
    "JJJJJ YYYYYY 3/0"+ "\n"+"KKKKKKK"+ "\n"+
    "UUU UUU UUUUU"+ "\n"+"QQQQQQQ"+ "\n"+"!");

    newline = '\n';
    excl = '!';
    input_taken = new StringBuffer (myinputstring)
    }


    public void parseString(StringBuffer x)
    { System.out.print(x.toString());}
    do stuff //use your delimiters }

    public static void main (String args[])
    { sort_pgragh test = new sort_pgragh();
    test.parseString(test.input_taken);
    }


    }

  9. #9
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    i had a few typos on class name

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If you have to post code then please use code tags and make sure to keep the code as far away from fire crackers as possible so that they don't format it like that.
    As far as the OP's problem is concerned, it is best to let them read the suggested tutorial and try a solution themselves. We can then help if they get stuck with their code.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    If you have to post code then please use code tags and make sure to keep the code as far away from fire crackers as possible so that they don't format it like that.
    Fire crackers don't leave a mess like that; that is definitely roadkill where the entire skunk family lost and got smeared all over the pavement, eeewww ...

    kind regards,

    Jos ;-)

  12. #12
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default My bad new to forum

    I don't even know what regex is.....and now I do know how to use the code tags ...but i believe i may have helped some other people today..I am really a j2se netbeans swing person and had no business posting that...give me a chance to learn the ways of the forum

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aaroncarpet View Post
    I don't even know what regex is.....and now I do know how to use the code tags ...but i believe i may have helped some other people today..
    Spoon feeding answers doesn't help, and spoon feeding bad answers actually hinders their education. Please be take care when answering here.

Similar Threads

  1. Java Regular Expression help
    By royalibrahim in forum Advanced Java
    Replies: 11
    Last Post: 11-12-2009, 01:27 AM
  2. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM
  3. Regular Expression Problem
    By daflores in forum Advanced Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM
  4. Quantifiers in Regular Expression
    By cdpm in forum New To Java
    Replies: 0
    Last Post: 12-24-2008, 01:03 PM
  5. Complex Regular Expression HELP
    By hiklior in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 01:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •