[英]Regex to split Camel case - with Numbers
I want to split a camelCase name to individual names using regex, for performing a spell check. 我想使用正则表达式将camelCase名称拆分为单个名称,以执行拼写检查。
The split should be as follows: 拆分应如下所示:
1) extendedStructureForNUB --> extended, Structure, For, NUB 1)extendedStructureForNUB->扩展,结构,用于NUB
2) extendedStructureFor2004 --> extended, Structure, For, 2004 2)extendedStructureFor2004->扩展,结构,用于2004
Using the answer from the below question , i am able to split for the 1st condition. 使用以下问题的答案,我可以为第一个条件分裂。
Question : RegEx to split camelCase or TitleCase (advanced) 问题 : RegEx拆分camelCase或TitleCase(高级)
But for a string containing number (2nd condition), it is not returning as per format. 但是对于包含数字的字符串(第二条件),它不会按照格式返回。
extendedStrctureFor2004 --> extended, Structure, For2004
Please suggest a way by which i can reuse this regex to split numerals also. 请提出一种我可以重用此正则表达式来拆分数字的方法。
public static void main(String[] args)
{
for (String w : "camelValue".split("(?<!(^|[A-Z0-9]))(?=[A-Z0-9])|(?<!^)(?=[A-Z][a-z])")) {
System.out.println(w);
}
}
Edit: Correcting the case for UPPER2000UPPER the regex becomes: 编辑:纠正UPPER2000UPPER的情况,正则表达式变为:
public static void main(String[] args)
{
for (String w : "camelValue".split("(?<!(^|[A-Z0-9]))(?=[A-Z0-9])|(?<!(^|[^A-Z]))(?=[0-9])|(?<!(^|[^0-9]))(?=[A-Za-z])|(?<!^)(?=[A-Z][a-z])")) {
System.out.println(w);
}
}
public static void main(String[] args)
{
for (String w : "extended2004FeeStructure".split("(?<!(^|[A-Z0-9]))(?=[A-Z0-9])|(?<!^)(?=[A-Z][a-z])")) {
System.out.println(w);
}
}
corrected one 更正的一个
What I see is answer from your previous question was almost pervect. 我看到的是上一个问题的答案几乎是完美的。 If I ware you i would just add another split opperation, but this time before first digit in middle in each word.
如果我知道您的意思,我只会添加另一个拆分操作,但是这次是在每个单词的中间第一个数字之前。
Here is example: 这是示例:
String data="2Hello2000WORLDHello2000WORLD";
//your previois split
String[] myFirstSplit=data.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])");
//I will store split results in list (I don't know size of array)
List<String> list=new ArrayList<>();
for (String s:myFirstSplit){
//if splited word contains digit after letter then split
for (String tmp:s.split("(?<=[a-zA-Z])(?=[0-9])"))
list.add(tmp);
}
System.out.println(list);
//out [2, Hello, 2000, WORLD, Hello, 2000, WORLD]
After you seperate this 分开之后
extendedStrctureFor2004 --> extended, Structure, For2004 extendedStrctureFor2004->扩展,Structure,For2004
Store it in some array like "arr" 将其存储在“ arr”之类的数组中
Use this Regex 使用此正则表达式
var numberPattern = /[0-9]+/g; var numberPattern = / [0-9] + / g; var numMatch= arr[i].match(numberPattern);
var numMatch = arr [i] .match(numberPattern);
now numMatch will contain the numerals u want.. 现在numMatch将包含您想要的数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.