开发者

Unexpected behaviour when trying to use String.split("\\?")

So I have a string that is like this:

"Some text here?Some number here"

and I need to split those, I am using String.split("\\?"), but if I have a string like this:

"This is a string with, comma?1234567"

I have it splitted in the comma(,) too. And if I have this String:

"That´s a problem here?123456"

It also 开发者_开发百科splits on ´, So how can I fix this?


I am not seeing this behaviour: (nor would I expect to)

String s ="hello?1000";

String[] fields = s.split("\\?");

for (String field : fields) {
   System.out.println(field);
}

yields:

hello

1000

Introducing a comma "," or an apostrophe "'" doesn't make any difference to the split:

String s ="he,llo?1000";

yields:

he,llo

1000

String s ="he'llo?1000";

yields:

he'llo

1000

The spilt also works fine if you have any spaces in your input string. I can only suggest that your regex is not what you think it is!


this is the solution: (EDIT: it's even simpler)

public static Pair<String,String> getSplittedByQuestionMark(String term){
    String[] list=term.split("[?]");
    return new Pair<String,String>(list[0],list[1]);
}

i tested it:

@Test
public void testGetSplittedByQuestionMark(){
    ArrayList<String> terms=new ArrayList<String>();
    ArrayList<Pair<String,String>> expected=new ArrayList<Pair<String,String>>();
    terms.add("test?a");
    terms.add("test?20");
    terms.add("test, with comma?ab10");
    expected.add(new Pair<String,String>("test","a"));
    expected.add(new Pair<String,String>("test","20"));
    expected.add(new Pair<String,String>("test, with comma","ab10"));
    for(int i=0;i<terms.size();i++){
        Pair<String,String> answer = StringStandardRegex.getSplittedByQuestionMark(terms.get(i));
        assertTrue("answer="+answer.getFirst(),answer.getFirst().equals(expected.get(i).getFirst()));
        assertTrue("answer="+answer.getSecond(),answer.getSecond().equals(expected.get(i).getSecond()));
    }

}

[EDIT after remark below] I have added a test, Now I don;t see what's the problem, this works as well (and is even more simpel):

@Test
public void testGetSplittedByQuestionMarkNotUsingRegex(){
    ArrayList<String> terms=new ArrayList<String>();
    ArrayList<Pair<String,String>> expected=new ArrayList<Pair<String,String>>();
    terms.add("test?a");
    terms.add("test?20");
    terms.add("test, with comma?ab10");
    expected.add(new Pair<String,String>("test","a"));
    expected.add(new Pair<String,String>("test","20"));
    expected.add(new Pair<String,String>("test, with comma","ab10"));
    for(int i=0;i<terms.size();i++){
        String[] answer=terms.get(i).split("\\?");
        assertTrue("answer="+answer[0],answer[0].equals(expected.get(i).getFirst()));
        assertTrue("answer="+answer[1],answer[1].equals(expected.get(i).getSecond()));
    }

}


Looks like a typical regex problem. I am using this for example to split

name (code)

into a pair with the name and the code separate:

RE regex = new RE("(.*) \\W(.*)\\W");
if(!regex.match(term)){
    throw new InvalidArgumentException("the given term does not match the regelar expression:'NAME (ID)'");
}
Pair<String,String> pair=new Pair<String,String>(regex.getParen(1),regex.getParen(2));
return pair;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜