• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Using RegEx to Strip Last Character from a String Conditionally

 
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the part number has an appended suffix preceeded by a space, strip the last character off.
If there is no preceeding space leave the lase character. Example: 41131001-299-101-AS

My question is can this be reduced(hopefully to a single line) using regex somehow?

 
Saloon Keeper
Posts: 10928
87
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

 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need it to return this:

System.out.println( trimPartNo( "41131001-299" ) );//Take out the "S"
System.out.println( trimPartNo( "41131001-299-101-AS" ) );
 
Marshal
Posts: 79952
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with substring()?
 
Carey Brown
Saloon Keeper
Posts: 10928
87
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

Steve Dyke wrote:I need it to return this:

System.out.println( trimPartNo( "41131001-299" ) );//Take out the "S"
System.out.println( trimPartNo( "41131001-299-101-AS" ) );


Do you mean, leave the spaces preceding the 'S' but remove only the 'S'?
 
Carey Brown
Saloon Keeper
Posts: 10928
87
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

Campbell Ritchie wrote:What's wrong with substring()?


It's not conditional.
 
Carey Brown
Saloon Keeper
Posts: 10928
87
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

Carey Brown wrote:

 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:
Do you mean, leave the spaces preceding the 'S' but remove only the 'S'?



Yes. Or remove spaces and "S".
Any character in the string that has a preceding space, I need to remove it.
Reason, the suffix is not a part of the Customer Part Number.
It is used internally within our ERP system to define certain attributes like routing process.
However, when the part number is referenced on any document going back to the Customer the suffix needs to be removed.
 
Carey Brown
Saloon Keeper
Posts: 10928
87
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

Steve Dyke wrote:Any character in the string that has a preceding space, I need to remove it.


Remove all instances of a space followed by a non-space.
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry.

I was doing this:


Instead of this:

 
Carey Brown
Saloon Keeper
Posts: 10928
87
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

Steve Dyke wrote:Sorry.
I was doing this:

Instead of this:


"replaceAll()" conflicts with the "$" meta-character which means match at end of string.
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
S/B replaceFirst...
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic