Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex to split string on condition

 
G Priya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<please ignore/>
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4437
256
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Priya India", please check your private messages regarding an important administrative matter.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like that could be a starting point:
 
G Priya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, James. Both would be the same under my requirement.

Thanks, Peter. I'll try this now.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:"Priya India", please check your private messages regarding an important administrative matter.

"Priya India", I really suggest you follow these instructions.
 
G Priya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just a starting point (it doesn't cover all possible characters) ... just to make sure you understand ; - )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic