Solved

Regular expressions - retain one substring, replace another

Posted on 2006-11-17
5
203 Views
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=
   [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
Comment
Question by:joex
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17964594
Try

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

Author Comment

by:joex
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.
0
 
LVL 86

Accepted Solution

by:
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");
0
 

Author Comment

by:joex
ID: 17965162
The following appears to work:

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

The "+" appears otherwise.

Thanks a lot!
0
 
LVL 86

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 49
oracle 11g 23 107
Html Table Looping (part 2) 5 27
Bot application - advice 3 38
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

856 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