• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Regex to Find String Inside Parentheses  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1784
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am needing to take a string and remove the characters between the parentheses.

I found this in one of my searches but will not work on string like:




My goal is to only return the 129.0 What I get is 129.016
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post real code. Your String isn't a String, and your second replaceAll() call isn't a valid method invocation.
 
Steve Dyke
Ranch Hand
Posts: 1784
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Please post real code. Your String isn't a String, and your second replaceAll() call isn't a valid method invocation.


Sorry.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's still not valid.  replaceAll takes two strings as arguments, and your second call only has one.
 
Steve Dyke
Ranch Hand
Posts: 1784
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:That's still not valid.  replaceAll takes two strings as arguments, and your second call only has one.


Sorry 1 more time.

String nResult = "WEIGHT 129.0 LBS. (DN16)- ESTIMATED DELIVERABLE SEAT";
 
nResult = nResult.replaceAll("\\(.*\\)", "");
 
nResult = nResult.replaceAll("[^0-9.]|\\.(?![0-9]","");
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The first replaceAll() call should correctly remove the parenthesis, along with the substring inside of it. The second replaceAll() call has a regex that is not valid, and should not work.

Regardless, those two method calls should not generate the result that you stated in your first post.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears to me that you can do this much more easily by just describing the entire input string using a Pattern, and then use named capturing groups to get the parts you're interested in.

In what ways does your input string change? If it's only the weight and the code between the parentheses, you can make capturing groups out of those.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of the 2nd replaceAll() why not use a Pattern that looks for a floating point number, e.g. "([-+]?[0-9]*\.?[0-9]+)", then use group() to get access to the match.
 
Steve Dyke
Ranch Hand
Posts: 1784
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Instead of the 2nd replaceAll() why not use a Pattern that looks for a floating point number, e.g. "([-+]?[0-9]*\.?[0-9]+)", then use group() to get access to the match.


I am taking another approach to this issue. I will be prompting the user for the item weight in a stand alone field so the match will not be necessary.

Thanks all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!