• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Use Regex to Group on Sub Strings  RSS feed

 
Ranch Hand
Posts: 1932
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an example of one of my strings:

SEAT AY-AAR CTR IAT IVS TPL

This is what I would like my result to be:

CTR IAT IVS TPL

My code:



Results I am getting:
SAT AAAR CTR IAT IVS TPL
 
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's enclosed in brackets  [....] are a list of characters, or [^....] a list of NOT characters. You seem to be mixing strings (r/h|l/h|...) inside a list of characters.

Without knowing more about your input patterns it would be hard to recommend a regex. For instance, does input always start with "SEAT " ?
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody may have a trickier way to do this with one line but this is what I came up with that may suit your needs.
 
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is probably a pure REGEX way to solve the problem, but this is how I would tackle it:
Edited to fix code typo
 
Bartender
Posts: 2212
92
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you meant :
you missed the space in SPACE.
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:you missed the space in SPACE.


Thanks for spotting that - fixing it now  
 
salvin francis
Bartender
Posts: 2212
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless I have completely misunderstood OP's question, here's a simple regex I wrote:

 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the OP knows the set of patterns he wants to accept and wants to reject everything else. Therefore we don't know if "WXYZ" will appear, or "ABCD", but if it's not one of the accepted patterns then don't include it. I can't think of a way to do this with replaceAll().
 
Steve Dyke
Ranch Hand
Posts: 1932
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I think that the OP knows the set of patterns he wants to accept and wants to reject everything else. Therefore we don't know if "WXYZ" will appear, or "ABCD", but if it's not one of the accepted patterns then don't include it. I can't think of a way to do this with replaceAll().



Yes, you have correctly interrupted my challenge. I will try your previous solution. Thanks.
 
Steve Dyke
Ranch Hand
Posts: 1932
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:There is probably a pure REGEX way to solve the problem, but this is how I would tackle it:
Edited to fix code typo



This would solve my challenge but we are not using Java 8 yet. Thanks.
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like Ron's example because it's clean and robust (but is Java-8).
My example suffers from matching things that are more than 3 characters, e.g. "SEAT AY-AAR CTR TPLQ XYZ". It will see "TPL" as a match even though it is "TPLQ", which is not a match.
A hybrid solution would work best, I think.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!