DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. JavaScript
  4. Fun with Regular Expressions: ANT-style Variable Replacing in Strings

Fun with Regular Expressions: ANT-style Variable Replacing in Strings

Peter Friese user avatar by
Peter Friese
·
Sep. 11, 09 · Interview
Like (0)
Save
Tweet
Share
13.90K Views

Join the DZone community and get the full member experience.

Join For Free

i recently felt the need to write a piece of code that resolves ant-style variables in a string. suppose you have a property file similar to this one:

propertya=somevalue
propertyb=${propertya}.someothervalue
listofthings=${propertya}, ${propertyb}, constantvalue

let's further assume you want to read property listofthings , and resolve the variables. isn't that a perfect job for regular expressions?

using regex tester , i came up with the following regular expression to find occurrence of ${ variable }:

\$\{(.+?)\}

using java.util.regex.pattern and java.util.regex.matcher to find all occurrences is rather trivial:

pattern re = pattern.compile("\\$\\{(.+?)\\}");
matcher m = re.matcher(sourcestring);
while (m.find()) {
  string variable = m.group(1);
  system.out.println(variable);
}

replacing the variables with their concrete values is not so trivial. you might be tempted to use string.substring():

value = sourcestring.substring(0, m.start()) + replacement + (sourcestring.substring(m.end()));

but this will modify the source string, basically throwing the matcher out of the curve.

looking at the matcher api, i found java.util.regex.matcher.appendreplacement(stringbuffer, string) and java.util.regex.matcher.appendtail(stringbuffer). these two little gems to the trick:

private string resolve(string sourcestring, properties props) {
  pattern re = pattern.compile("\\$\\{(.+?)\\}");
  matcher m = re.matcher(sourcestring);
  stringbuffer result = new stringbuffer();
  while (m.find()) {
    string variable = m.group(1);
    string resolved = resolve(props.getproperty(variable), props);
    m.appendreplacement(result, resolved);
  }
  m.appendtail(result);
  return result.tostring();
}

regular expressions do save the day!

from http://www.peterfriese.de

Strings

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Integrating AWS Secrets Manager With Spring Boot
  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • What To Know Before Implementing IIoT
  • Implementing PEG in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: