replaceall

Sanitize HTML string

狂风中的少年 提交于 2019-12-25 04:13:41
问题 I have an HTML sting like: <p dir="ltr"><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u>bold</u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i></b></u></i><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b><i><u><b>

java replaceAll(regex, replacement) regex command

徘徊边缘 提交于 2019-12-25 03:24:35
问题 I would like to know if anyone knows the regex command to remove the following name = from the following name = wlr leaving only wlr these details are taken from a txt file but the name = part can appear multiple times So I was thinking something like this would work but it doesn't work properly String file_name = newLine3.replaceAll("name = ", ""); 回答1: String file_name = newLine3.replaceAll("name\\s+=\\s+", ""); 回答2: String newLine3 = "name = wlr"; String fileName = newLine3.replaceAll(

Cannot to replace a string to another (boolean value) - Java

偶尔善良 提交于 2019-12-24 07:46:00
问题 boolean b1 = false; boolean b2 = true; String s = new String(b1+""+b2); s.replaceAll("false", "f"); s.replaceAll("true", "t"); Nothing is replaced. I still get "false true". I want to replace all "false" and "true", of the String, to "f" and "t". Im pretty sure Im not doing it the right way, and thats why I need your help, which will be greatly appreciated. 回答1: String in java is immutable, which means, once created, the value of a String object will not change. The method replaceAll() (or

Escaping double-slashes with regular expressions in Java

感情迁移 提交于 2019-12-23 12:55:02
问题 I have this unit test: public void testDeEscapeResponse() { final String[] inputs = new String[] {"peque\\\\u0f1o", "peque\\u0f1o"}; final String[] expected = new String[] {"peque\\u0f1o", "peque\\u0f1o"}; for (int i = 0; i < inputs.length; i++) { final String input = inputs[i]; final String actual = QTIResultParser.deEscapeResponse(input); Assert.assertEquals( "deEscapeResponse did not work correctly", expected[i], actual); } } I have this method: static String deEscapeResponse(String str) {

Regex to replace a repeating string pattern

我与影子孤独终老i 提交于 2019-12-22 16:13:10
问题 I need to replace a repeated pattern within a word with each basic construct unit. For example I have the string "TATATATA" and I want to replace it with "TA". Also I would probably replace more than 2 repetitions to avoid replacing normal words. I am trying to do it in Java with replaceAll method. 回答1: I think you want this (works for any length of the repeated string): String result = source.replaceAll("(.+)\\1+", "$1") Or alternatively, to prioritize shorter matches: String result = source

Java: Understanding the String replaceAll() method

左心房为你撑大大i 提交于 2019-12-21 05:06:54
问题 I'm looking to figure out the answer to this problem here. First off, blah[abc] = blah[abc].replaceAll("(.*) (.*)", "$2, $1"); Can someone explain to me what the (.*), $2 and $1 are? Secondly, when I nest that within a for statement in order to reverse two parts of a string, I am hit with an exception error. I was wondering if anybody knew why that is. Thanks Edit: This is the error I receive Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at ChangeNames.main

java String.replaceAll regex question

让人想犯罪 __ 提交于 2019-12-20 07:38:42
问题 I have a string that contains the following text String my_string = "hello world. it's cold out brrrrrr! br br"; I'd like to replace each isolated br with <br /> The issue is that I'd like to avoid converting the string to "hello world. it's cold out <br />rrrrr! <br /> <br />"; What I'd like to do is convert the string (using replaceAll) to "hello world. it's cold out brrrrrr! <br /> <br />"; I'm sure this is very simple, but my regex isn't correct. my_string.replaceAll("\\sbr\\s|\\sbr$", "

Printing a replaced line in a new text file

只愿长相守 提交于 2019-12-20 07:21:19
问题 I am trying to edit a matlabfile and replace some of the coding parts init in some specific lines. However, using the format below to make the changes it wont change the line context at all. (it will print the same old line). Any idea what am I doing wrong? 'replaceAll' is not suitable to replace some words with some others in the lines? Thanks in advance. try { PrintWriter out = new PrintWriter(new FileWriter(filenew, true)); Scanner scanner = new Scanner(file); while (scanner.hasNextLine())

String.replaceAll() and \n line breaks

可紊 提交于 2019-12-20 06:47:48
问题 I try to explain my problem with a little example. I implemented version 1 and version 2, but I didn't get the desired result. Which replacement-parameter do I have to use to get the desired result with the replaceAll method ? Version 1: String s = "TEST"; s = s.replaceAll("TEST", "TEST\nTEST"); System.out.println(s); Output: TEST TEST Version 2: String s = "TEST"; s = s.replaceAll("TEST", "TEST\\nTEST"); System.out.println(s); Output: TESTnTEST Desired Output: TEST\nTEST 回答1: From the

Java: replaceAll doesn't work well with backslash?

放肆的年华 提交于 2019-12-20 04:24:26
问题 I'm trying to replace the beginning of a string with backslashes to something else. For some weird reason the replaceAll function doesn't like backslashes. String jarPath = "\\\\xyz\\abc\\wtf\\lame\\"; jarPath = jarPath.replaceAll("\\\\xyz\\abc", "z:"); What should I do to solve this issue. Thank you. 回答1: You need to double each backslash (again) as the Pattern class that is used by replaceAll() treats it as a special character: String jarPath = "\\\\xyz\\abc\\wtf\\lame\\"; jarPath = jarPath