Results 1 to 5 of 5
  1. #1
    hariz2410 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Regular Expression Help needed PLease!!

    HI all

    I am very new to java, but i have this problem i got a string e.g

    Courses['07001'].Title = "International Students 1";

    So i need to extract only the "international Students", i did that by using this expression
    String regex = "Courses\\[\'[^\']+\'\\]\\.Title *= *\"([^\"]+)\";";

    But the problem now is, say i got string like

    Courses['07001'].CO = new Array("MR","","Harish Ram","Not Available","M.R Harish","hotmail.com");

    so i need to extract"07001", "MR Harish Ram " and extract "M.RHarish @ hotmail.com" note that i need to add the @ when compiling. got any ideas if you could help me to give a solution of how to retrieve the name and email add seperately...

    So my final output should look something like " "07001", "MR Harish Ram","M.RHarish @ hotmail.com""

    Help is greatly appreciated

    Thanks in advance!!!!!

    Please Help!!

    harish
    Last edited by hariz2410; 12-14-2010 at 07:38 PM.

  2. #2
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default

    Well, a quick solution could be just to use .split(",") instead of regex. It may not the the most elegant solution, but would work for your example. yourString.split(",") would return an array of strings and the last two entries in that array would be "M.R Harish" and "hotmail.com"); which you can easily substring to what you need.

    This is a very quick solution, but will only works if your string format does not vary.

    More proper way would be, like you suggested, with regex...but I'll let a regex expert tackle that one :)

  3. #3
    hariz2410 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the reply, but i need one more thing as well sorry forgot about that my final output should be something like this.
    ""07001", "MR Harish Ram","M.RHarish @ hotmail.com""

    Please help

    Thanks in advance

    Harish
    Last edited by hariz2410; 12-14-2010 at 07:39 PM.

  4. #4
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default

    well, same idea, if yourString is " Courses['07001'].CO = new Array("MR","","Harish Ram","Not Available","M.R Harish","hotmail.com"); " then applying strArray = yourString.split("\"") will be a good start.

    strArray[1] will have "Courses['07001'].CO = new Array("
    strArray[2] will have "MR"
    strArray[6] will have "Harish Ram"
    strArray[10] will have "M. R. Harish"
    strArray[12] will have "hotmail.com"

    then apply the same trick to strArray[1], but with "'" as the split parameter (ex. strArray[1].split("'")) and the second value of that will be "07001".

    With that you can piece your final string together.

    Again, not as pretty as regex, but an easy solution if you need a quick fix. Hope it helps.

  5. #5
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    :D

    I'm not sure if I understood you correctly, but you could use groups or?
    quick (and mybe little dirty :D ) :
    Java Code:
    String regex = "Courses\\['(\\d+)'\\]\\..+ = new Array\\((.*?)\",(.*?),\"(.*?),(.*?),(.*?)\",\"(.*?)\\)";
    so you could use group(1) to group(7) and you will get
    Java Code:
    07001
    "MR
    ""
    Harish Ram"
    "Not Available"
    "M.R Harish
    hotmail.com"
    (some quote signs are wrong because : see next :D )
    the full code:
    Java Code:
    		String s = "Courses['07001'].CO = new Array(\"MR\",\"\",\"Harish Ram\",\"Not Available\",\"M.R Harish\",\"hotmail.com\")";
    		String regex = "Courses\\['(\\d+)'\\]\\..+ = new Array\\((.*?)\",(.*?),\"(.*?),(.*?),(.*?)\",\"(.*?)\\)";
    		Matcher m = Pattern.compile(regex).matcher(s);
    		if(m.find()){
    			//System.out.println("\""+m.group(1)+"\""+","+m.group(2)+" "+m.group(4)+","+m.group(6)+" @ "+m.group(7));
    			for (int i = 1; i < 8; i++) {
    				System.out.println(m.group(i));
    			}
    		}
    the commented line prints
    Java Code:
    "07001","MR Harish Ram","M.R Harish @ hotmail.com"
    is that correct? ;/

Similar Threads

  1. Help with regular expression
    By mr.ab18 in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 10:01 PM
  2. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 11:15 AM
  3. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 03:55 AM
  4. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 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
  •