-3

enter image description here

I tried this with a regex tester and it works. But why can't i get it work for JAVA. I am hoping to search for neither..........nor

I want to count how many neither somethingnor are in a string : "Neither u or me are human".

I have tried :

occurrence += sentence.split( "(?i)\\Wneither.+nor\\W" ).length - 1;

but it is not working because the output System.out.print(occurrence) result is 0.

I thought \\W stands for non-word character while .+ means any character(s).

How can I get a occurrence result of 1?

g0rdonL
  • 301
  • 1
  • 3
  • 14
  • *this is not working* is not a problem description, and *any solution* to a problem you've not explained is not a question. What **specific problem** are you having with that regex, and what **specific question** would you like us to answer for you? – Ken White Apr 06 '17 at 03:31
  • There are zero occurrences of "neither.+nor" in "Neither u or me are human". Also at least 1 occurrence of grammar errors :-) – sprinter Apr 06 '17 at 03:32
  • If you're looking for `neither ABC nor` (where _ABC_ is any word) then it's correct that your `"Neither u or me are human"` gives result of zero. If the string was `"Neither u nor me are human"` then you could expect result of 1, isn't it? Is your question really just _"How to count how many `neither` and how many `nor` are used in sentence"_? – VC.One Apr 06 '17 at 20:06

1 Answers1

1

You can count occurrences with this:

Pattern pattern = Pattern.compile("(neither|nor)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("Neither u or me are human");

int count = 0;
while (matcher.find())
    count++;

System.out.println(count);
Sergey
  • 176
  • 2
  • 12
  • Asker has updated (changed the rules) of expected result. They want specifically (`neither` + `*` + `nor`) where `*` is anything between the two words _neither_ and _nor_ or at least to my understanding – VC.One Apr 06 '17 at 20:01