java - How to add text in between a sentence using regex? -
my input
<option value="" disabled selected hidden>
the output should this:
<option value="" disabled="disabled" selected="selected" hidden="">
then tried code;
final string regex_disabled = "(?<=option value=\"\" disabled)(?=.*)"; final string replace_disabled = "=\"disabled\""; pattern disp = pattern.compile(regex_disabled); matcher dism = disp.matcher(text); text = dism.replaceall(replace_disabled); final string regex_selected = "(?<==\"disabled\" selected)(?=.*)"; final string replace_selected = "=\"selected\""; pattern selp = pattern.compile(regex_selected); matcher selm = selp.matcher(text); text = selm.replaceall(replace_selected); final string regex_hidden = "(?<==\"selected\" hidden)(?=.*)"; final string replace_hidden = "=“”"; pattern hidp = pattern.compile(regex_hidden); matcher hidm = hidp.matcher(text); text = hidm.replaceall(replace_hidden);
it worked since asked more simple, hoping if find useful , more simple since tried apply other ways won't work , tried searching other ways.
try this:
"<option(.*?)\\s+(disabled)\\s+(selected)\\s+(hidden)>"
java sample
final string regex = "<option(.*?)\\s+(disabled)\\s+(selected)\\s+(hidden)>"; final string string = "<option value=\"\" disabled selected hidden>\n\n" + "<option value=\"adfsa\" disabled selected hidden>\n\n" + "<option value=\"111\" disabled selected hidden>\n\n\n\n"; final string subst = "<option $1 $2=\"disabled\" $3=\"disabled\" $4=\"hidden\">"; final pattern pattern = pattern.compile(regex); final matcher matcher = pattern.matcher(string); // substituted value contained in result variable final string result = matcher.replaceall(subst); system.out.println("substitution result: " + result);
Comments
Post a Comment