Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

java - Regex creation to be used on replaceAll

I am trying to use String.replaceAll(regex, String) to replace some chars from a String.

My string is some html and I've been trying to replace 'width: xxxxx.xxxpx;' width 'width: auto;'.

I came up with 2 reg expressions which according to an online reg tester works fine as:

width: [0-9]{3,}px; -- match at least 3 digits followed by px;
width: [0-9]{3,}.[0-9]{1,}px; -- match at least 3 digits followed by any char (in my case a .) followed by at least 1 digit and finally px;

This has been used in this way in my code:

text = text.replaceAll("width: [0-9]{3,}px;", "width: auto;");
text = text.replaceAll("width: [0-9]{3,}.[0-9]{1,}px;", "width: auto;");

as an example, I used this text:

<p style="margin-left: 10px;"><br></p><p style="margin-left: 10px;">text&nbsp;<span style="text-decoration-line: underline;">not</span>&nbsp;not text.</p><div class="ui-resizable-handle ui-resizable-n" style="width: 1404.8px; z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-w" style="height: 100.8px; z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-e" style="height: 100.8px; z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="width: 1404.8px; z-index: 90;"></div><table class="ui-resizable" style="width: 1405.6px; border: 1px solid black; margin-left: auto; margin-right: auto;"><tbody><tr><td class="ui-resizable" style="padding: 10px; border: 1px solid black;">in my opinion both this text is wrong;<br><br><br><div class="ui-resizable-handle ui-resizable-e" style="height: 99.2px; z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="width: 1404px; z-index: 90;"></div></td></tr></tbody></table><p style="margin-left: 10px;">other text:&nbsp;</p><table class="ui-resizable" style="width: 1422.4px; border: 1px solid black; margin-left: auto; margin-right: auto;"><tbody><tr><td class="ui-resizable" style="padding: 10px; border: 1px solid black;"><p><br></p><p><br></p></td></tr></tbody></table>

But this is not working. What am I doing wrong, please?

question from:https://stackoverflow.com/questions/65849198/regex-creation-to-be-used-on-replaceall

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Are you sure about your code, wouldn't it be something like?

text = text.replaceAll("width: [0-9]{3,}px;", "width:auto");
text = text.replaceAll("width: [0-9]{3,}.[0-9]{1,}px;", "width:auto");

Also I would change the second Regex by adding a backslash before the dot

"width: [0-9]{3,}.[0-9]{1,}px;"

And what do you get as result string with your current code?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...