Regular expression Remove spaces from a group

Hi, I have the following meanings

000001010016C02AB 111* 000001010016C02 111H 000001010016C 111 

And the expected result

 00000101001,C02AB,* 00000101001,C02,H 00000101001,C, 

Values โ€‹โ€‹may vary. The length of this line will always be 23. If the character is absent, the position will be filled with a space. Regex i have now

 (^.{11})[0-9](.{5})(?:.{5})(.*) 

But when using this regular expression in the second group, white spaces are returned. I want these white spaces to be removed.

Current output:

 00000101001,C02AB,* 00000101001,C02 ,H 00000101001,C , 

Can someone help me remove spaces from the second group?

Demo

+5
source share
4 answers
  • Find: ^(.{11})\d(\S+)\s*.{3}(.?)$
  • Replace: $1,$2,$3

Explanation:

 ^ : beginning of string (.{11}) : 11 any character, stored in group 1 \d : 1 digit (\S+) : 1 or more non spaces, stored in group 2 \s* : 0 or more spaces .{3} : 3 any character (.?) : 0 or 1 character, stored in group 3 $ 

Result:

 00000101001,C02AB,* 00000101001,C02,H 00000101001,C, 
+1
source

In Java, you can implement custom replacement logic using Matcher#appendReplacement() and just trim() value of matcher.group(2) :

 String strs[] = {"000001010016C02AB 111*", "000001010016C02 111H", "000001010016C 111 ", "901509010012V 154 "}; Pattern p = Pattern.compile("(.{11})[0-9](.{5}).{5}(.*)"); for (String s: strs) { StringBuffer result = new StringBuffer(); Matcher m = p.matcher(s); if (m.matches()) { m.appendReplacement(result, m.group(1) + "," + m.group(2).trim() + "," + m.group(3)); } System.out.println(result.toString()); } 

Result:

 00000101001,C02AB,* 00000101001,C02,H 00000101001,C, 90150901001,V, 

See the Java demo .

Note. I deleted ^ because the Matcher#matches() method requires full string matching. Use the Pattern.DOTALL parameter if the string may contain line breaks.

+1
source

There are capture groups in Regex, just combine these 2 groups and you will get your results, in concatenation you can insert a comma

  ^(\w+)\s*\d+(\D+)$ 

A group is what's inside ()

0
source

Using the final $ statement makes matching easier:

 ^(.{11})\d(\w+).+(.)$ 
0
source

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


All Articles