Results 1 to 5 of 5
- 12-14-2010, 07:05 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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!!
harishLast edited by hariz2410; 12-14-2010 at 07:38 PM.
- 12-14-2010, 07:27 PM #2
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 :)
- 12-14-2010, 07:33 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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
HarishLast edited by hariz2410; 12-14-2010 at 07:39 PM.
- 12-14-2010, 08:45 PM #4
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.
- 12-14-2010, 09:46 PM #5
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
:D
I'm not sure if I understood you correctly, but you could use groups or?
quick (and mybe little dirty :D ) :
so you could use group(1) to group(7) and you will getJava Code:String regex = "Courses\\['(\\d+)'\\]\\..+ = new Array\\((.*?)\",(.*?),\"(.*?),(.*?),(.*?)\",\"(.*?)\\)";
(some quote signs are wrong because : see next :D )Java Code:07001 "MR "" Harish Ram" "Not Available" "M.R Harish hotmail.com"
the full code:
the commented line printsJava 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)); } }
is that correct? ;/Java Code:"07001","MR Harish Ram","M.R Harish @ hotmail.com"
Similar Threads
-
Help with regular expression
By mr.ab18 in forum New To JavaReplies: 2Last Post: 08-06-2010, 10:01 PM -
regular expression
By prof.deedee in forum JDBCReplies: 3Last Post: 02-19-2010, 11:15 AM -
regular expression
By QkrspCmptPop in forum Advanced JavaReplies: 8Last Post: 01-20-2010, 03:55 AM -
regular expression
By ras_pari in forum Advanced JavaReplies: 27Last Post: 10-07-2009, 12:25 PM -
Complex Regular Expression HELP
By hiklior in forum New To JavaReplies: 1Last Post: 04-30-2008, 01:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks