Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to get a RegEx to extract only uppercase from string

 
Bartender
Posts: 4006
156
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Saloon Keeper
Posts: 12155
258
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 12155
258
  • Mark post as helpful
  • send pies
  • 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
  • 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: 7202
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is more straightforward.
 
Stephan van Hulst
Saloon Keeper
Posts: 12155
258
  • Mark post as helpful
  • send pies
  • 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: 7202
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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: 7202
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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: 69854
278
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Regexes are fun!

(So are Streams...)
 
Stephan van Hulst
Saloon Keeper
Posts: 12155
258
  • Mark post as helpful
  • send pies
  • 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
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic