正则表达式从组中删除空格

时间:2022-03-05 19:20:10

Hi I have the following values

嗨,我有以下数值

000001010016C02AB  111*
000001010016C02    111H
000001010016C      111 

And the expected output is

而预期的产出是

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

The values might vary.The length of this string will always be 23.if a character is not present then the position will be a filled with a white space. The Regex now i have is

值可能会有所不同。此字符串的长度始终为23.如果字符不存在,则位置将填充空白区域。现在的正则表达式是

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

But while using this Regex in the second group there are white spaces returned. I want those those white spaces to be removed.

但是在第二组中使用此正则表达式时,会返回空格。我希望那些白色空间被删除。

Current Output:

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

Could anyone help me remove the white spaces from the second group?

任何人都可以帮我删除第二组的空白区域吗?

Demo

4 个解决方案

#1


1  

  • 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, 

#2


1  

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

在Java中,您可以使用Matcher#appendReplacement()实现自定义替换逻辑,只需修剪()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.

请参阅Java演示。

Note I removed ^ because Matcher#matches() method requires a full string match. Use the Pattern.DOTALL option if the string may contain line breaks.

注意我删除了^因为Matcher#matches()方法需要完整的字符串匹配。如果字符串可能包含换行符,请使用Pattern.DOTALL选项。

#3


0  

In Regex there are capturing groups, just concatenate these 2 groups and you'll have your results, in the concatenation you may insert a comma

在正则表达式中有捕获组,只是连接这两个组,你将得到你的结果,在连接中你可以插入一个逗号

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

A group is what is inside ()

一个组是什么内部()

#4


0  

Using the end assertion $ makes it easy to match:

使用结束断言$可以很容易地匹配:

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

#1


1  

  • 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, 

#2


1  

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

在Java中,您可以使用Matcher#appendReplacement()实现自定义替换逻辑,只需修剪()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.

请参阅Java演示。

Note I removed ^ because Matcher#matches() method requires a full string match. Use the Pattern.DOTALL option if the string may contain line breaks.

注意我删除了^因为Matcher#matches()方法需要完整的字符串匹配。如果字符串可能包含换行符,请使用Pattern.DOTALL选项。

#3


0  

In Regex there are capturing groups, just concatenate these 2 groups and you'll have your results, in the concatenation you may insert a comma

在正则表达式中有捕获组,只是连接这两个组,你将得到你的结果,在连接中你可以插入一个逗号

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

A group is what is inside ()

一个组是什么内部()

#4


0  

Using the end assertion $ makes it easy to match:

使用结束断言$可以很容易地匹配:

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