• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to get a RegEx to extract only uppercase from string

 
Bartender
Posts: 5584
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
et cetera, et cetera.

@Mike,

can you supply us with some sentences that formed the motivation of your opening post, and tell in each case what should be the outcome?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Darryl Burke wrote:What's wrong with sentence.replaceAll("(?:\\b)[a-z]*(?:\\b)", "")?  And if the double spaces left behind are an issue, that could be chained to .replace("  ", " ").


That regular expression does not take into account that the sentence could consist of characters other than a-z, A-Z and spaces.


Ah, I see.

Stephan van Hulst wrote:Also, the second replace would have to be performed multiple times until there are no more changes.


Um, no.

String#replace(CharSequence target, CharSequence replacement) wrote:Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but if you have three spaces in a row, it will replace the first two with a single space, but the two spaces that are left over will not be replaced, because the matcher has moved past the first one (which was a replacement).
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a short program that illustrates what I mean:

Output:
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephan, I see where I went wrong.

Still assuming that only words with all-lowercase (i.e. [a-z]) are to be removed, how about this?
 
Bartender
Posts: 10980
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
This is more straightforward.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey, that suffers from the same problem. If the replacement results in multiple spaces in a row, they won't be recursively replaced.
 
Carey Brown
Bartender
Posts: 10980
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

Stephan van Hulst wrote:Carey, that suffers from the same problem. If the replacement results in multiple spaces in a row, they won't be recursively replaced.


Look again. Doesn't need to be recursive.

Try
 
Carey Brown
Bartender
Posts: 10980
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

The "\\s" means any white space character. The "+" says one or more. So you have a replacement pattern of one or more adjacent spaces to be replaced by a single space.
 
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't been following this th‍read carefully; once it go onto the second page it started to get long‑winded. But a long time ago somebody showed how to extract upper case letters from the String with a Stream. So why do you insist on wanting a regex?
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regexes are fun!

(So are Streams...)
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:
The "\\s" means any white space character. The "+" says one or more. So you have a replacement pattern of one or more adjacent spaces to be replaced by a single space.


Yes, but it won't replace the replacements that result in multiple spaces in a row.
Nevermind, it was a long weekend
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic