我不知道为什么我在这件事上挣扎得这么厉害,但任何帮助都会非常感谢。
我正在创建自己的标记器,它接受一个包含命令、分隔符和值列表的文件。然后,它输出每个"token“以及它的类型。
输入:AND 3, 4, 5 ; some comments
我需要输出:
AND --- command
3 --- value
, --- delimiter
4 --- value
, --- delimiter
5 --- value我现在让它在我输出的地方工作:
AND 3, 4, 5 --- delimiter但我需要进一步分析它。
这是我目前所在的位置:
ArrayList<Token> tokenize(String[] input) {
ArrayList<Token> tokens = new ArrayList<Token>();
for (String str : input) {
Token token = new Token(str.trim());
//Check if int
try{
Integer.parseInt(str);
token.type = "number";
} catch(NumberFormatException e) {
}
if (token.type == null) {
if (commands.contains(str))
token.type = "command";
else if (str.contains(",")) {
token.type = "delimiter";
} else if (destValues.contains(str))
token.type = "destination";
else
token.type = "unknown";
}
if(! token.type.equals("unknown"))
tokens.add(token);
}
return tokens;
}对于这个赋值,我唯一真正的限制是不能使用StringTokenizer和正则表达式。
发布于 2012-10-26 05:26:27
看起来你的输入不正确。尝试使用此方法拆分输入,然后使用标记化方法。
import java.util.*;
public class Foo {
public static void main( String[] args ) {
String input = "AND 3, 4, 5 ; some comments";
List<String> parts = new ArrayList<String>();
// removing comments
input = input.split( ";" )[0];
// splits using spaces
String[] firstPass = input.trim().split( " " );
for ( String s : firstPass ) {
// the current part cannot be empty
if ( !s.trim().isEmpty() ) {
// splits using comma
String[] secondPass = s.split( "," );
for ( String ss : secondPass ) {
parts.add( ss.replace( ",", "" ) );
}
// verifies if the current part has a comma
// and if so, inserts it as a part
if ( s.contains( "," ) ) {
parts.add( "," );
}
}
}
for ( String a : parts ) {
System.out.println( a );
}
}
}编辑:当我的第一个anwer工作时,这里是一个完整的例子,其中有一些重构...
import java.util.*;
public class MyTinyParser {
private static final String COMMANDS = "AND OR FOO BAR";
private List<String> extract( String input ) {
List<String> parts = new ArrayList<String>();
// removing comments
input = input.split( ";" )[0];
// splits using spaces
String[] firstPass = input.trim().split( " " );
for ( String s : firstPass ) {
// the current part cannot be empty
if ( !s.trim().isEmpty() ) {
// splits using comma
String[] secondPass = s.split( "," );
for ( String ss : secondPass ) {
parts.add( ss.replace( ",", "" ) );
}
// verifies if the current part has a comma
// and if so, inserts it as a part
if ( s.contains( "," ) ) {
parts.add( "," );
}
}
}
return parts;
}
public List<Token> tokenize( String input ) {
List<Token> tokens = new ArrayList<Token>();
for ( String str : extract( input ) ) {
Token token = new Token( str );
// check if int
try{
Integer.parseInt( str );
token.type = "number";
} catch(NumberFormatException e) {
}
if ( token.type == null ) {
if ( COMMANDS.contains(str)){
token.type = "command";
} else if (str.contains(",")) {
token.type = "delimiter";
} else {
token.type = "unknown";
}
}
if( !token.type.equals( "unknown" ) ) {
tokens.add( token );
}
}
return tokens;
}
private class Token {
String value;
String type;
Token( String value ) {
this.value = value;
}
@Override
public String toString() {
return String.format( "Token[%s, %s]", value, type );
}
}
public static void main( String[] args ) {
MyTinyParser mtp = new MyTinyParser();
List<Token> tokens = mtp.tokenize( "AND 3, 4, 5 ; some comments" );
for ( Token t : tokens ) {
System.out.println( t );
}
}
}发布于 2012-10-26 05:37:58
如果你被允许使用谷歌的api,你也可以尝试类似下面的东西。
import com.google.common.base.Splitter;
public class Tmp {
public static void main(String[] args) {
String str = "AND 3, 4, 5 ; some comments";
Iterable<String> stringIterable = Splitter.on(' ').trimResults()
.omitEmptyStrings()
.split(str);
for (String str1 : stringIterable) {
int commaIndex = str1.indexOf(",");
if (commaIndex > 0) {
System.out.println(str1.subSequence(0, commaIndex));
System.out.println(",");
} else {
System.out.println(str1);
}
}
}
}它打印
AND
3
,
4
,
5
;
some
comments附言:不是最好的代码。它可以进一步改进,人们可以自由地加入进来。
https://stackoverflow.com/questions/13077232
复制相似问题