Replace a substring of a matching regular expression

I am extracting some html and doing some string manipulation and writing to a string like

string sample = "\n \n 2 \n \n \ndl. \n \n \n flour\n\n \n 4 \n \n cups of \n\nsugar\n" 

I would like to find all lines of ingredients and remove spaces and line breaks

2 dl. flour and 4 cups of sugar

My approach so far is as follows.

 Pattern p = Pattern.compile("[\\d]+[\\s\\w\\.]+"); Matcher m = p.matcher(Result); while(m.find()) { // This is where i need help to remove those pesky whitespaces } 
+6
source share
6 answers

The following code should work for you:

 String sample = "\n \n 2 \n \n \ndl. \n \n \n flour\n\n \n 4 \n \n cups of \n\nsugar\n"; Pattern p = Pattern.compile("(\\s+)"); Matcher m = p.matcher(sample); sb = new StringBuffer(); while(m.find()) m.appendReplacement(sb, " "); m.appendTail(sb); System.out.println("Final: [" + sb.toString().trim() + ']'); 

OUTPUT

 Final: [2 dl. flour 4 cups of sugar] 
+3
source

sample = sample.replaceAll("[\\n ]+", " ").trim();

Conclusion:

2 dl. flour 4 cups of sugar

No spaces at the beginning and no spaces at the end.

First, it replaces all spaces and newlines with one space, and then cuts out the extra space from the request / end.

+4
source

I think something like this will work for you:

 String test = "\n \n 2 \n \n \ndl. \n \n \n flour\n\n \n 4 \n \n cups of \n\nsugar\n"; /* convert all sequences of whitespace into a single space, and trim the ends */ test = test.replaceAll("\\s+", " "); 
+1
source

I assumed that \n not the actual line feed, but also works with linefeeds . This should work fine:

test=test.replaceAll ("(?:\\s|\\\n)+"," ");

If there is no textual \n , this could be simpler:

test=test.replaceAll ("\\s+"," ");

And you need to trim leading / trailing spaces.

I use the RegexBuddy tool to test any single regular expression that is very convenient for many languages.

+1
source

You should be able to use the standard String.replaceAll (String, String) . The first parameter will take your template, the second an empty string.

0
source
 s/^\s+//s s/\s+$//s s/(\s+)/ /s 

Run these three substitutions (replacing leading spaces with nothing, replacing trailing spaces with nothing, replace several spaces with a space.

0
source

Source: https://habr.com/ru/post/889146/