Regular expressions - retain one substring, replace another

Posted on 2006-11-17
Last Modified: 2006-11-18
In order to change the minimum font size in HTML to be 3, for a string such as:

   Font face="Times New Roman" size=1

it would be helpful to be able use two substrings:

   Font face=\"[A-Za-z0-9-]*\" size=

and then retain the the first substring in the replace, while modifying the second substring.

in some versions of vi, this could be done with something like:

  s/\(Font face=\"[A-Za-z0-9-]*\" size=\)\([12]\)/\13

Please indicate whether something similar can be done in java.


Question by:joex
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 86

Expert Comment

ID: 17964594

String s = "Font face=\"Times New Roman\" size=1";
s = s.replaceAll("(Font face=\"Times New Roman\" size=)(\\d+)", $1+"3");

Author Comment

ID: 17965098
The above won't compile - the $1 is not recognized
Also, perhaps it replaces all sizes with 3 rather than just sizes 1/2.
LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 17965127
>>The above won't compile - the $1 is not recognized

Sorry -that should be

, "$1+3");

>>Also, perhaps it replaces all sizes with 3 rather than just sizes 1/2.

s = s.replaceAll("(Font face=\"Times New Roman\" size=)([1-2])", "$1+3");

Author Comment

ID: 17965162
The following appears to work:

   s.replaceAll("(Font face=\"Times New Roman\" size=)([1-2])", "$13");

The "+" appears otherwise.

Thanks a lot!
LVL 86

Expert Comment

ID: 17965187
Good - nice to see a fellow vi user ;-)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question