Re: Regex string with quotes
Hello,
The regex does may not necessarily be relevant in this case, may be a simple split may be more effective. Just have a look at the following code.
Code:
String s = "\"chain\" 2 \"another sing\"";
String[] chspt = sing.split("\"");
for(String subsing: chspt){
if (! subsing.equals(""))
System.out.System.out.println(subsing);
}
You can try this code and if you have any problems then you can post and please post with the error you get.
Re: Regex string with quotes
Hello,
The above code is correct and you should try that, alternatively with the regex, you can do that, but I am not convinced that is a super as a solution:
Code:
{
Pattern ptn = Pattern.compiles("([^\"]*)");
Matcher mth = ptn.mth("\"chain\" 2 \"another string\"");
while (mth.find()) {
if (! mth.group().equals(""))
System.out.System.out.println(mth.group());
}
You can try both the code this one and the above post and see which one is suitable for your program.
Re: Regex string with quotes
Hello,
Thank you very much for your reply. Little precision that it is the method split (on string) also uses pattern so it is not slower and you can then use the expression Unfortunately, your solutions do not work because if you take the string following the result is incorrect
Code:
"string" 1 2 "another string"
If you have any other solution to suggest it will be great. Thanks in advance.
Re: Regex string with quotes
Hello,
I have a code with me, though I am not sure it will work with your program that is I have no idea about your exact needs. Just go through it and if you feel it will work with you, ten you can use it in your program.
Code:
int itr = 0;
while (i <chaineSplit.size()) {
chain[itr] = "";
if (chaineSplit.get(i).startsWith("\"")) {
while (! chaineSplit.get(i).endsWith("\"")) {
chain[itr] + = chsp.get(i) + " ";
i + +;
}
}
chain[itr] + = chsp.get(i);
i + +;
itr + +;
}
for (String s: string)
if (s! = null)
System.out.System.out.println(s);
Re: Regex string with quotes
Hello,
Here is the code, you can have a look at it
Code:
Public static List <String> getWords(String input) {
ArrayList rst = <String> new ArrayList <String>();
Pattern pat = Pattern.compiles("((?<!\\\\)\".*?(?<!\\\\)\")|([^\"\\s] +) ");
Matcher mth = pat.mth(input);
while (mth.find()) {
rst.add(mth.group());
}
rst.TrimToSize();
return rst;
}