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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
mergeTwo  challenge 13 72
mapBully challenge 6 89
topping2 challenge 13 59
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now