如何将字符串中非数字文本分割为字符,但保留所有数字?

huangapple go评论78阅读模式
英文:

How to split a none digit text inside a String into charterers but keep all the numbers?

问题

import re

input_string = "ab:123+234-345*456///+ hi100"
regex_pattern = r"|(?<=\s)|(?<=\D)(?=\d)|(?<=\d)(?=\D)|((?<=\s+)|(?=\s+))|^\D"
result = re.split(regex_pattern, input_string)

output = 
展开收缩
print(output)
英文:

I have a string mixed with digits and none digits, how to split the none digits (including Space) into character but keep all the number

Input:

ab:123+234-345*456///+ hi100

Output:

[&quot;a&quot;, &quot;b&quot;, &quot;:&quot;,&quot;123&quot;, &quot;+&quot;, &quot;234&quot;,&quot;-&quot;, &quot;345&quot;, &quot;*&quot;,&quot;456&quot;, &quot;/&quot;, &quot;/&quot;,&quot;/&quot;, &quot;+&quot;, &quot; &quot;, &quot;h&quot;, &quot;i&quot;, &quot;100&quot;]  

I tried this

regx = &quot;|(?&lt;=\\s)|(?&lt;=\\D)(?=\\d)|(?&lt;=\\d)(?=\\D)|((?&lt;=\\s+)|(=\\s+))|^\\D&quot;;

答案1

得分: 3

以下是翻译好的内容:

你可以使用以下正则表达式的交替部分进行匹配:

  • \d+:匹配1个或更多数字
  • \D:匹配一个非数字

将此匹配用于while循环中。

代码:

final String regex = "\\d+|\\D";
final String string = "ab:123+234-345*456///+hi100";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

List<String> result = new ArrayList<>();

while (matcher.find()) {
    result.add(matcher.group());
}

System.out.println(result);

或者,你可以使用以下正则表达式来进行split操作:

String[] result = str.split("(?!^)(?=\\D)|(?<=\\D)(?=\\d)");

正则表达式演示链接

英文:

You may use this regex with an alternation for matching:

\d+|\D

There are 2 alternatives in this regex:

  • \d+: Match 1+ digits
  • \D: Match a non-digit

Use this match in a while loop.

RegEx Demo

Code:

final String regex = &quot;\\d+|\\D&quot;;
final String string = &quot;ab:123+234-345*456///+hi100&quot;;

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

List&lt;String&gt; result = new ArrayList&lt;&gt;();

while (matcher.find()) {
      result.add ( matcher.group() );
}

System.out.println( result );

Alternatively you may use this regex for split:

(?!^)(?=\D)|(?&lt;=\D)(?=\d)

RegEx Demo

Code:

String[] result = str.split( &quot;(?!^)(?=\\D)|(?&lt;=\\D)(?=\\d)&quot; );

答案2

得分: 3

你可以使用正则表达式 \d+|\D,它表示要么是数字(即 \d+),要么是非数字字符(\D)。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        String str = "ab:123+234-345*456";
        Pattern pattern = Pattern.compile("\\d+|\\D");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

输出:

a
b
:
123
+
234
-
345
*
456
英文:

You can use the regex, \d+|\D which means either digits (i.e. \d+) or (i.e. |) a non-digit (\D).

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

	public static void main(String[] args) {
		String str = &quot;ab:123+234-345*456&quot;;
		Pattern pattern = Pattern.compile(&quot;\\d+|\\D&quot;);
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			System.out.println(matcher.group());
		}
	}
}

Output:

a
b
:
123
+
234
-
345
*
456

答案3

得分: 0

你可以尝试这个:

\D+?|\d+

+? - 表示匹配一个重复项的非贪婪模式
以及数字 + 作为贪婪匹配

参见:regex101

英文:

You can try this one:

\D+?|\d+

+? - is lazy match for 1 repeater
and digits + from greedy match

see: regex101

答案4

得分: 0

这是一个不使用正则表达式的解决方案:

private List<String> split(String input) {
  List<String> result = new ArrayList<>();
  StringBuilder sb = new StringBuilder();
  for (char c : input.toCharArray()) {
    if (Character.isDigit(c)) {
      sb.append(c);
    } else {
      if (sb.length() > 0) {
        result.add(sb.toString());
        sb.setLength(0);
      }

      result.add(String.valueOf(c));
    }
  }
  if (sb.length() > 0) {
    result.add(sb.toString());
  }
  return result;
}
英文:

Here is a solution without using regular expressions:

private List&lt;String&gt; split(String input) {
  List&lt;String&gt; result = new ArrayList&lt;&gt;();
  StringBuilder sb = new StringBuilder();
  for (char c : input.toCharArray()) {
    if (Character.isDigit(c)) {
      sb.append(c);
    } else {
      if (sb.length() &gt; 0) {
        result.add(sb.toString());
        sb.setLength(0);
      }

      result.add(String.valueOf(c));
    }
  }
  if (sb.length() &gt; 0) {
    result.add(sb.toString());
  }
  return result;
}

huangapple
  • 本文由 发表于 2020年10月18日 21:57:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64414152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定