0

I want to create a Java regex to find all words that have a single quote inside them. Words examples :

'word'

'Word

'W'ord

'W'or'd

Word'

w'orD

w'o'r'd

I hope i am not missing any case above. Currently i am using this query :

[a-zA-Z]*('{1,})[a-zA-Z]*('{0,})[a-zA-Z]*

But it still fails to grab all the words' cases above. Is there a regular expression to find if a word has one or more single quote ?

Note: In my case i can't use Java methods like String.contains(...). I want it to be a regular expression solution.

Community
  • 1
  • 1
Brad
  • 4,457
  • 10
  • 56
  • 93

2 Answers2

0

You can probably use:

(?=.*?[a-z])(?=.*?')[a-z']+

Online Demo: http://regex101.com/r/dR7gS9

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This will also match any other normal words like about, search, results, ... !! – Brad Mar 04 '14 at 07:46
  • Sorry that link is not showing any test input. Can you provide me regex101 link – anubhava Mar 04 '14 at 15:13
  • I apologize. I see regex101 does not support java regex. Please check this screenshot: http://i.imgur.com/4q9vFJy.jpg?1 – Brad Mar 04 '14 at 15:35
  • Thanks for screenshot. But it appears to be matching all your inputs, is that not what you expect? – anubhava Mar 04 '14 at 15:52
  • Look at the bottom of the screenshot. Additional words are matched like "The" . – Brad Mar 04 '14 at 17:02
  • It will be better if you update your question with your sample input and expected outputs. From your present set of inputs it appears you have one word per line but apparently not (as evident from screenshot). – anubhava Mar 04 '14 at 17:06
0

try this

    Matcher m = Pattern.compile("['?\\p{L}]+").matcher(s);
    while(m.find()) {
        System.out.println(m.group());
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275