• 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

Use Regex to Group on Sub Strings

 
Ranch Hand
Posts: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 4510
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you meant :
you missed the space in SPACE.
 
Ron McLeod
Marshal
Posts: 4510
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless I have completely misunderstood OP's question, here's a simple regex I wrote:

 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

reply
    Bookmark Topic Watch Topic
  • New Topic