Java-拆分字符串,为字母数字 [英] Java- Split String which is alphanumeric

查看:46
本文介绍了Java-拆分字符串,为字母数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例输入:

RC23
CC23QQ21HD32
BPOASDf91A5HH123

示例输出:

[RC,23]
[CC,23,QQ,21,HD,32]
[BPOASDf,91,A,5,HH,123]

字母部分和数字部分的长度不固定.

The length of alpha part and numeric part is not fixed.

我知道如何将split()与正则表达式一起使用,例如//.'''([[a-z]),但是尽管我检查了 split() Java API,但找不到任何可以帮助我解决此问题的东西.

I know how to use split() with regex like //.' ' ' '([a-z]) but although I checked split() Java API I can't find anything that can help me to solve this problem.

是否可以使用 split()来做到这一点?或者我需要使用另一种方法来分割这些字符串.

Is there a way to use split() to do this? Or I need to use another method to split these string.

任何帮助将不胜感激.

推荐答案

尝试以下正则表达式:((?< = [a-zA-Z])(?= [0-9]))|((?< = [0-9])(?= [a-zA-Z]))"

Try this regex: "((?<=[a-zA-Z])(?=[0-9]))|((?<=[0-9])(?=[a-zA-Z]))"

这是一个正在运行的示例: http://ideone.com/c02rmM

Here's a running example: http://ideone.com/c02rmM

{
    ...
    String someString = "CC23QQ21HD32";
    String regex = "((?<=[a-zA-Z])(?=[0-9]))|((?<=[0-9])(?=[a-zA-Z]))";
    System.out.println(Arrays.asList(someString.split(regex)));
    //outputs [CC, 23, QQ, 21, HD, 32]
    ...
}

正则表达式使用 lookahead (?= ValueToMatch) lookshins (?< = ValueToMatch).

The regex is using lookahead (?=ValueToMatch) and look behinds (?<=ValueToMatch).

它的前半部分(在|之前)问:上一个字符是字母(?< = [a-zA-Z])?下一个字符是数字吗?(?= [0-9])?"如果两者都为真,则将字符串与正则表达式匹配.

The first half of it (before the | ) is asking: "Is the previous character a letter (?<=[a-zA-Z])? Is the next character a digit (?=[0-9])?" If both are true, it'll match the string to the regex.

该正则表达式的后半部分正相反.它问:前一个字符是数字(?< = [0-9])?下一个字符是字母吗?(?= [a-zA-Z])",如果两者都为真,它将再次匹配.

The second half of that regex is doing it the other way around. It asks: "Is the previous character a digit (?<=[0-9])? Is the next character a letter? (?=[a-zA-Z])", and again it'll match if both are true.

通常split()会删除正则表达式匹配的字符.即使对于此正则表达式也是如此.但是,由于正则表达式与0宽前瞻匹配,因此不会删除您要查找的实际字符.

Normally the split() would remove the characters matched by the regex. This remains true even to this regex. However, since the regex is matching a 0-width lookahead, the actual characters you're looking for are not removed.

请查看亚当·佩恩特(Adam Paynter)的答案,以获取有关 lookaheads look backs 的更多信息:

Check out Adam Paynter's answer for more on lookaheads and look behinds: how to split string with some separator but without removing that separator in Java?

这篇关于Java-拆分字符串,为字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆