File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Regex to split string on condition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regex to split string on condition" Watch "Regex to split string on condition" New topic
Author

Regex to split string on condition

G Priya
Greenhorn

Joined: Apr 30, 2007
Posts: 11
Hi, It would be good if someone can help me with a regex problem am facing.

I need to compare two types of strings.

Type1 : This type of string has several substrings in it enclosed within '{$' and '$}'.
Example:
My name is {$Jill$}. I work on {$Java$} development.

Type2 : This type of string has several substrings in it enclosed within '#{' and '}'.
Example:
My name is #{Jack}. I work on #{C++} development.

When comparing string of type1 with string of type2, I want to ignore the substrings within {$...$} in type1 and those within #{...} in type2, and then compare. In the above example, ignoring the substrings within those specified characters and then comparing would yield that they are equal strings. This is the comparison that I want to do. Can you please guide hoe to do it in regex?

For type1, I tried this:
Pattern pattern = Pattern.compile("\\{.*\\}");
pattern.split(stringType1);

But this doesn't seem to work. Kindly guide.

Thanks.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

<please ignore/>


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3100
    
  40

"Priya India", please check your private messages regarding an important administrative matter.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Something like that could be a starting point:


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
G Priya
Greenhorn

Joined: Apr 30, 2007
Posts: 11
Yes, James. Both would be the same under my requirement.

Thanks, Peter. I'll try this now.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Priya India wrote:Yes, James. Both would be the same under my requirement.


I withdrew my response because I realised there was much much more to the problem than I originally thought. If the two strings I provide truly were to be considered the same then you don't want to split the lines. You just want to remove the {$ ... $} and #{ ... } elements and then compare the resulting strings.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Devaka Cooray wrote:"Priya India", please check your private messages regarding an important administrative matter.

"Priya India", I really suggest you follow these instructions.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
G Priya
Greenhorn

Joined: Apr 30, 2007
Posts: 11
Am sorry for not noticing the msg on the private msg. I took note of it. Thanks.

Peter, Thanks so much! It works!
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
It's just a starting point (it doesn't cover all possible characters) ... just to make sure you understand ; - )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex to split string on condition