13

I am looking for a regex that matches first word in a sentence excluding punctuation and white space. For example: "This" in "This is a sentence." and "First" in "First, I would like to say \"Hello!\""

This doesn't work:

"""([A-Z].*?(?=^[A-Za-z]))""".r
4
  • 3
    What flavour of regex is this? Commented Feb 8, 2013 at 6:41
  • Can the words have numbers in them?
    – endy
    Commented Feb 8, 2013 at 6:41
  • ([a-z]+), case-insensitive, should be sufficient for "non-tricky" English .. however, it will fail for non-latin characters quickly - so update to use Unicode character classes as appropriate! Note that this assumes an NFA regex (like Ruby :D) which will "match the first thing it can", but that works in favor here as there is no need to anchor or otherwise complex look-arounds.
    – user166390
    Commented Feb 8, 2013 at 6:57
  • Start of a sentence or start of a string, like in your examples? What is about e.g. "It's not a good idea!" or "Fürchterlichéß Beispiel." (just an example!)?
    – stema
    Commented Feb 8, 2013 at 8:54

5 Answers 5

14
(?:^|(?:[.!?]\s))(\w+)

Will match the first word in every sentence.

http://rubular.com/r/rJtPbvUEwx

3
  • 1
    "123 This doesnt work" as it will return "123" instead of "This"
    – konyak
    Commented Mar 29, 2013 at 19:09
  • That is because that is the first word. Like OP had asked. If you want it to match the first dictionary word then you should be looking someplace other then regex.
    – endy
    Commented Mar 30, 2013 at 17:13
  • 1
    Would you kindly explain what everything before (\w+) does, please?
    – Nubarke
    Commented Oct 14, 2015 at 11:44
5

This is an old thread but people might need this like I did. None of the above works if your sentence starts with one or more spaces. I did this to get the first (non empty) word in the sentence :

(?<=^[\s"']*)(\w+)

Explanation:

(?<=^[\s"']*) positive lookbehind in order to look for the start of the string, followed by zero or more spaces or punctuation characters (you can add more between the brackets), but do not include it in the match.
(\w+) the actual match of the word, which will be returned

The following words in the sentence are not matched as they do not satisfy the lookbehind.

4

You can use this regex: ^[^\s]+ or ^[^ ]+.

3

You can use this regex: ^\s*([a-zA-Z0-9]+).

The first word can be found at a captured group.

2
[a-z]+

This should be enough as it will get the first a-z characters (assuming case-insensitive).

In case it doesn't work, you could try [a-z]+\b, or even ^[a-z]\b, but the last one assumes that the string starts with the word.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.