英文:
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:
["a", "b", ":","123", "+", "234","-", "345", "*","456", "/", "/","/", "+", " ", "h", "i", "100"]
I tried this
regx = "|(?<=\\s)|(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)|((?<=\\s+)|(=\\s+))|^\\D";
答案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.
Code:
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 );
Alternatively you may use this regex for split
:
(?!^)(?=\D)|(?<=\D)(?=\d)
Code:
String[] result = str.split( "(?!^)(?=\\D)|(?<=\\D)(?=\\d)" );
答案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 = "ab:123+234-345*456";
Pattern pattern = Pattern.compile("\\d+|\\D");
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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论