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 Video Library
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
View Events Video Library
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
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Parallelism in ConcurrentHashMap
  • Python F-Strings
  • Java String: A Complete Guide With Examples
  • How to Automatically Detect Multiple Cybersecurity Threats from an Input Text String in Java

Trending

  • Future Skills in Cybersecurity: Nurturing Talent for the Evolving Threatscape
  • Auditing Spring Boot Using JPA, Hibernate, and Spring Data JPA
  • An Introduction to Build Servers and Continuous Integration
  • How to Migrate Vector Data from PostgreSQL to MyScale
  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
14.00K 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.

Related

  • Parallelism in ConcurrentHashMap
  • Python F-Strings
  • Java String: A Complete Guide With Examples
  • How to Automatically Detect Multiple Cybersecurity Threats from an Input Text String 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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: