• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

replaceAll method

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw this line of code to parse a calculation giving as a String such as "15+15".
i

Can someone explain what it does in detail?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first argument to replaceAll is a regular expression - a pattern that consists of rules that determine how to find a piece of text that matches the pattern.

The pattern ([-+*/]) means: one of the characters -, +, * or /

The call in your example replaces each -, +, * or / by " $1 ". So if input would be "15+15" then the result of the call would be "15 $1 15".

It's followed by a call to split the resulting string on spaces, so you'd end up with a String[] containing three strings: "15", "$1", "15".
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the result will be:
"15", "+", "15"

javadoc wrote:Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string

 
Saloon Keeper
Posts: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the second argument is a so called "replacement string". The dollar sign is used to reference capturing groups in the regular expression. "$1" refers to group 1; in this case the replaceAll() call is used to put spaces around the operators -, +, * and /.

The result of evaluating the expression for the string "15+15" would be {"15", "+", "15"}.
 
Chris Yates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's pretty cool, thanks!
 
rubbery bacon. rubbery tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic