-1

I have a source code file which I am trying to read using a automatic Regex processor Class in java.

Although I am unable to form a correct regex pattern to get the values if it appears multiple times in the line.

The input text is:

<input name="id" type="radio" bgcolor="<bean:write name='color'/>" value="<bean:write name='nameProp' property='nameVal'/>" <logic:equal name="checkedStatus" value="0">checked</logic:equal>>

And I want the matcher.find to output following terms:

<bean:write name='color'/>
<bean:write name='nameProp' property='nameVal'/>

Kindly help to form the regex pattern for this scenario.

Anupam
  • 19
  • 3
  • Ask yourself why you want to use regex for this. If the answer isn't "my professor is making us", then you probably really want to reconsider your design decision. – dcsohl Mar 21 '16 at 15:11

1 Answers1

0

Use this regex to find those terms:

<bean:write[^\/]*\/>

It will search for the words <bean:write and then everything up until a />

Use it like this:

List<String> matches = new ArrayList<>();
Matcher m = Pattern.compile("<bean:write[^\\/]*\\/>")
                   .matcher(inputText);
while (m.find()) {
    matches.add(m.group());
}

Regex101 Tested

I caution you though with parsing HTML with regex. If you need anything more complicated than this, you should probably consider using an XML parser instead. See this famous answer.

Community
  • 1
  • 1
4castle
  • 32,613
  • 11
  • 69
  • 106