0

I have a html file in which the html elements have name as follows :

<input type="text" name="HCFA_DETAIL_SUPPLEMENTAL" value="" size="64" />

My requirement is to rename the name attribute value in java naming convention as follows :

<input type="text" name="hcfaDetailSupplemental" value="" size="64" />

Since there are large number of such elements, I want to accomplish that using regex. Can anyone suggest my how to achieve that using regex ?

Manoj Shrestha
  • 4,246
  • 5
  • 47
  • 67
  • 1
    Do not use regexes to parse HTML. Use a proper library. – m0skit0 May 29 '12 at 09:36
  • Actually, It's a velocity template, where html tags are embedded in it. So, does the html parser work for vm templates as well ? If yes, can you please list out the names of some of the parsers ? If not, I just wanted to treat the vm template as an ordinary file where a file parser parses it and returns its content as a string. And then I want to modify the values of name attributes and again rewrite the content into another file. – Manoj Shrestha May 29 '12 at 09:43

3 Answers3

1

Do not use regular expressions to go over HTML (why here). Using an appropriate framework such as HTML Parser should do the trick.

A series of samples to get you started are available here.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
1

Using jQuery to get the name, and then regexes to replace all the _[a-z] occurances:

$('input').each(function () {
    var s = $(this).attr('name').toLowerCase();
    while (s.match("_[a-z]"))
        s = s.replace(new RegExp("_[a-z]"), s.match("_[a-z]").toString().toUpperCase());
    $(this).attr('name', s);
});
Hidde
  • 11,493
  • 8
  • 43
  • 68
1

In most cases using regex with html is bad practice, but if you must use it, then here is one of solutions.

So first you can find text in name="XXX" attribute. It can be done by using this regex (?<=name=")[a-zA-Z_]+(?="). When you find it, replace "_" by "" and don't forget to lowercase rest of letters. Now you can replace old value by new one using same regex we used before.

This should do the trick

String html="<input type=\"text\" name=\"HCFA_DETAIL_SUPPLEMENTAL\" value=\"\" size=\"64\"/>";

String reg="(?<=name=\")[a-zA-Z_]+(?=\")";
Pattern pattern=Pattern.compile(reg);
Matcher matcher=pattern.matcher(html);
if (matcher.find()){
    String newName=matcher.group(0);
    //System.out.println(newName);
    newName=newName.toLowerCase().replaceAll("_", "");
    //System.out.println(newName);
    html=html.replaceFirst(reg, newName);
}
System.out.println(html);
//out -> <input type="text" name="hcfadetailsupplemental" value="" size="64"/>
Pshemo
  • 122,468
  • 25
  • 185
  • 269