识别在Java
编号系统/序列/图案在一堆串的我有这样这些识别在Java
1. INTRODUCTION
2. BASICS
3. ADVANCED CONCEPTS
4. EXAMPLES
一串串的上面的每个行是一个单独的字符串。如下 -
A. INTRODUCTION
B. BASICS
C. ..
OR为
I) INTRODUCTION
II) BASICS
III) ...
OR为
10.01 INTRODUCTION
10.02 BASICS
...
所以,我试图找出(和潜在地消除)任何类型的序列相同的字符串可以出现(数字,浮动,罗马数字和完全未知的类型)在这些字符串之间退出。 在java中这样做的最好方法是什么?
你想分裂中间空间吗?
public class TestApp {
public static void main(String[] args) {
String[] strings = new String[] {
"1. INTRODUCTION",
"2. BASICS",
"3. ADVANCED CONCEPTS",
"4. EXAMPLES"};
for(String string : strings) {
String[] tokens = string.split(" ");
System.out.println("[" + string + "][" + tokens[0] + "][" + tokens[1] + "]");
}
}
}
输出是
[1. INTRODUCTION][1.][INTRODUCTION]
[2. BASICS][2.][BASICS]
[3. ADVANCED CONCEPTS][3.][ADVANCED]
[4. EXAMPLES][4.][EXAMPLES]
如果你知道你的模式用一个简单的设计模式,这样
public class TestApp {
private static IPatternStripper[] STRIPPERS = new IPatternStripper[] {
new NumeralStripper()
// more types here ...
};
public static void main(String[] args) {
String[] strings = new String[] {
"1. INTRODUCTION",
"2. BASICS",
"3. ADVANCED CONCEPTS",
"4. EXAMPLES"};
for(String string : strings) {
IPatternStripper foundStripper = null;
for(IPatternStripper stripper : STRIPPERS) {
if(stripper.isPatternApplicable(string)) {
foundStripper = stripper;
break;
}
}
if(foundStripper != null) {
System.out.println("SUCCESS: " + foundStripper.stripPattern(string));
}
else {
System.out.println("ERROR: NO STRIPPER CAN PROCESS: " + string);
}
}
}
}
interface IPatternStripper {
public boolean isPatternApplicable(String line);
public String stripPattern(String line);
}
class NumeralStripper implements IPatternStripper {
@Override
public boolean isPatternApplicable(String line) {
boolean result = false;
// code here checks whether this stripper is appropriate
return result;
}
@Override
public String stripPattern(String line) {
String value = line;
// code here to do your stripping
return value;
}
}
没有必要存在空间。 – Jay
你知道你的订购模式吗? – Constantin
我正在尝试处理数百个可能有任何模式的文档。但正常模式是我上面列出的模式。有像1)简介或(A)简介或i.INTRODUCTION的情景。即使你坚持使用数字,数字和字母,也可以有很多排序/变化。 – Jay
[你尝试过什么?](http://mattgemmell.com/你有什么尝试/) – RealSkeptic
有扫描仪类可以帮助你,http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html –
你想解析这些字符串,但我不明白y你想要做的事情,提供一个输入/输出的例子。 – m0skit0