Re: Tricky regex question



On Jan 31, 10:54 am, Juan Singh <j...@xxxxxxxxxxxxx> wrote:
Hi,

I need to split words from a sentence that are more than 2 letters. An
example sentence is:

This is "very" 'tricky'. I won't be able see it.

I came up with the following regex, but it breaks the word [won't] and I
only get the part that is before the single quote.

\b[a-zA-Z]{2,}+\b

My objective is to extract the following words from the example sentence
above.

This, very, tricky, won't, able

Thanks.
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
public static void main(String[] args) {
Pattern longword = Pattern.compile("\\w+('?\\w){2,}");
String input = "This is \"very\" 'tricky'. I won't be able see
it.";
Matcher matcher = longword.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}

.



Relevant Pages

  • Re: Tricky regex question
    ... I need to split words from a sentence that are more than 2 letters. ... This is "very" 'tricky'. ... Matcher matcher = longword.matcher; ...
    (comp.lang.java.programmer)
  • Re: word count - letter count (newbie)
    ... > I'm trying to use 'wc' to count some letters in a tricky way. ... A Problem-Solution Approach, 2005, Apress ...
    (comp.unix.misc)
  • Re: Blank Posts
    ... Leashes are tricky little SOBS, ... http://www.cat-sidh.net (the Mother Ship) ... http://esther.cat-sidh.net (Letters to Esther) ...
    (rec.pets.dogs.behavior)
  • Opinion needed on new font
    ... I desigend a new font that proved so tricky that I need outside help. ... attempts have evolved into an group of letters, ...
    (comp.fonts)
  • Re: Tricky regex question
    ... I need to split words from a sentence that are more than 2 letters. ... This is "very" 'tricky'. ... trim, but rather than removing leading or trailing whitespace, it removes ...
    (comp.lang.java.programmer)