• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex to split string on condition

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<please ignore/>
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Priya India", please check your private messages regarding an important administrative matter.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like that could be a starting point:
 
G Priya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just a starting point (it doesn't cover all possible characters) ... just to make sure you understand ; - )
 
reply
    Bookmark Topic Watch Topic
  • New Topic