-6

I have a String like this

String s1="1 AND 1 OR 1";

I want split with AND OR and my result should be 1,1,1

I am using JAVA /eclipse package com.test;

String s1="1 AND 1 OR 1";

String[] splits=s1.split("[AND\OR]");

for (int i = 0; i < splits.length; i++) {

System.out.println(splits[i]);

}

}

}

Can I get any help how to do this?

Any Help appreciated

  • This question appears to be off-topic because it is about regular expressions, totally language specific, and doesn't specify the mandatory complementary tag – Denys Séguret Jun 16 '14 at 12:30
  • Can you share the code that you have tried? – Braj Jun 16 '14 at 12:30
  • possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) - Scroll to *other* and see `\s` – HamZa Jun 16 '14 at 12:30
  • 2
    Which language/tool are you using for your regex? – anubhava Jun 16 '14 at 12:31
  • I am using JAVA /eclipse package com.test; public class tesdt { public static void main(String[] args) { /*String str="1 AND 1"; int[] my=new int[100]; String[] s= str.split("AND"); for (int i = 0; i < str.length(); i++) { my[i]=Integer.parseInt(s[i].trim()); }*/ String s1="1 AND 1 OR 1"; String[] splits=s1.split("[AND\\OR]"); for (int i = 0; i < splits.length; i++) { System.out.println(splits[i]); } } } – user3628323 Jun 16 '14 at 13:05

1 Answers1

-1

I am unsure about the language, but for C# you can use the following:

string s1 = "1 AND 1 OR 1";
string s2 = s1.Replace("AND", ",").Replace("OR", ",");
Console.WriteLine(s2);

Which doesn't use regular expressions.

If you want an array, you can use the following:

string s1 = "1 AND 1 OR 1";
string[] s2 = Regex.Split(s1.Replace(" ", string.Empty), "AND|OR");

In Java you can replace using the same mechanism:

String s1 = "1 AND 1 OR 1";
String s2 = s1.replace("AND", ",").replace("OR", ",");
System.out.println(s2);

And to get an array:

String s1="1 AND 1 OR 1";
String[] s2 = s1.replace(" ", "").split("AND|OR");
Matthijs
  • 3,062
  • 3
  • 19
  • 41