Current location: Hot Scripts Forums » Programming Languages » Everything Java » Regular Expression


Regular Expression

Reply
  #1 (permalink)  
Old 03-27-08, 01:20 AM
mohit's Avatar
mohit mohit is offline
Newbie Coder
 
Join Date: Jul 2006
Location: India
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Regular Expression

Hi all,

I want to do some reverse parsing on string using regular expression.
see e.g.
string is : "cancer of lung as cancer example to get cancer."

i want to write a regular expression which can return me
"cancer" only in case "cancer" following "of lung".

can We parse it using single regular expression?

Regards,
Mohit
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-27-08, 02:10 AM
mohit's Avatar
mohit mohit is offline
Newbie Coder
 
Join Date: Jul 2006
Location: India
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
I got it.

i can use this

[a-zA-Z]+(?=( )of( )lung)

but take the below example

"lung cancer of lung is called as something of lung "

now my requirement is i want "cancer lung" and "something lung" through one regular expression.



any help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-27-08, 06:24 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
you mean like this:
Code:
([a-zA-Z]+\s*(?=lung))
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-27-08, 09:32 AM
mohit's Avatar
mohit mohit is offline
Newbie Coder
 
Join Date: Jul 2006
Location: India
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your quick reply.
but
it is returning me only "of" instead of either side text.
i mean i need "cancer lung" from "cancer of lung".

any guess.
Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-28-08, 05:29 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
ow, sorry, I thought the string was "cancer lung". Well then!

This should do:
Code:
(([a-zA-Z]+)(?:\sof)?\s(?=lung))
this will result in ('cancer of lung', 'cancer') or ('something lung', 'something')
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 04-01-08, 02:53 AM
mohit's Avatar
mohit mohit is offline
Newbie Coder
 
Join Date: Jul 2006
Location: India
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry to say but i want "cancer lung" from "cancer of lung".

your expression is returning me cancer of or something of
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 04-02-08, 06:02 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Did you take a look at the Capturing Groups Tutorial? The second matched group will contain "cancer" or "something". If all strings you want to find end with "lung", you can simply select the second matched group, and add "lung" to it, like so:
Java Code:
  1. Pattern p = Pattern.compile("(([a-zA-Z]+)(?:\sof)?\s(?=lung))");
  2. Matcher m = Pattern.matcher (
  3. "lung cancer of lung is called as something of lung ");
  4. System.out.println (m.group(1) + " lung"); // it might be 2, depending on how Java numbers the groups
  5.  
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 04-03-08, 08:07 AM
mohit's Avatar
mohit mohit is offline
Newbie Coder
 
Join Date: Jul 2006
Location: India
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks,

but i have done it in something different way.
i used this

([a-zA-Z]+/[A-Z]+) (of/[A-Z]+) ([a-zA-Z]+/[A-Z]+)

then use replace it with $1 $3

Code:
String regex ="([a-zA-Z]+/[A-Z]+) (of/[A-Z]+) ([a-zA-Z]+/[A-Z]+)";
Pattern pattern=Pattern.compile(regex);
Matcher matcher= pattern.matcher(string);
while(matcher.find()){
		String str="";
                str= string.substring(matcher.start(),matcher.end());
                 str = pattern.matcher(str).replaceAll("$1 $3");
}
it worked for me.

thanks for contribution
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 04-11-08, 07:23 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
that will match any 3 word sentence, not just "cancer of lung"

Anyway, I'm glad you got it solved
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 05-08-08, 05:55 AM
hem1234 hem1234 is offline
New Member
 
Join Date: May 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Cool

Quote:
Originally Posted by mohit View Post
Thanks,

but i have done it in something different way.
i used this

([a-zA-Z]+/[A-Z]+) (of/[A-Z]+) ([a-zA-Z]+/[A-Z]+)

then use replace it with $1 $3

Code:
String regex ="([a-zA-Z]+/[A-Z]+) (of/[A-Z]+) ([a-zA-Z]+/[A-Z]+)";
Pattern pattern=Pattern.compile(regex);
Matcher matcher= pattern.matcher(string);
while(matcher.find()){
		String str="";
                str= string.substring(matcher.start(),matcher.end());
                 str = pattern.matcher(str).replaceAll("$1 $3");
}
it worked for me.

thanks for contribution
Thanks for the code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with regular expression... :) eddyvlad PHP 2 01-08-06 11:10 PM
Regular Expression help zoic PHP 4 12-02-05 07:15 PM
regular expression for parsing url link musmanm80 Perl 1 07-28-05 12:58 AM
Regular Expression KHWright Visual Basic 0 06-02-05 07:40 PM
suggest a regular expression gmadhukarreddy Perl 2 01-08-04 01:56 AM


All times are GMT -5. The time now is 04:24 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.