[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Regular expressions - retain one substring, replace another

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=
   [12]

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.

Thanks.

0
joex
Asked:
joex
  • 3
  • 2
1 Solution
 
CEHJCommented:
Try

String s = "Font face=\"Times New Roman\" size=1";
s = s.replaceAll("(Font face=\"Times New Roman\" size=)(\\d+)", $1+"3");
0
 
joexAuthor Commented:
The above won't compile - the $1 is not recognized
Also, perhaps it replaces all sizes with 3 rather than just sizes 1/2.
0
 
CEHJCommented:
>>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");
0
 
joexAuthor Commented:
The following appears to work:

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

The "+" appears otherwise.

Thanks a lot!
0
 
CEHJCommented:
Good - nice to see a fellow vi user ;-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now