0

I encounter a problem like this. I successfully create a pattern containing a word that is built from word-character only (no number, special character). It looks like this ^[^\d\W]*[^\d\W]*[^\d\W]$ but it works for 1 word only as below:

# "abcd" match
# "ab8d" don't match (contain a number)
# "aaaaaaaaa" match
# "aaaa!aaa" don't match (contain !)

Now I want to "extend" the pattern so it will work with a whole sentence "abcd ab8d aaaaaaaa aaaaa!aaaa" which will match "abcd" and "aaaaaaaa". Actually, I found the solution on Regex expression to capture only words without numbers or symbols, the pattern is (?<!\S)[A-Za-z]+(?!\S) BUT I still want to do it in my own way, since it will make me remember longer than just copy-paste code from the internet.

So my question is, if possible, is there any way to "extend" my pattern ^[^\d\W]*[^\d\W]*[^\d\W]$ to make it work for the whole sentence instead of 1 word? I'm thinking about handling the " " character between two words.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    But don't the whitespace boundaries suffice? Also, if you need to only match letters between whitespaces/start or end of string, you need `(?<!\S)[^\W\d_]+(?!\S)`. – Wiktor Stribiżew Mar 18 '22 at 15:04
  • 1
    Mind that `^[^\d\W]*[^\d\W]*[^\d\W]$` is equal to `^[^\d\W]*[^\d\W]$` and matches a string that contains any zero or more letters or underscores and ends with a `_` or a letter. Do you really want to match "words" like that? – Wiktor Stribiżew Mar 18 '22 at 15:17
  • 1
    Ok, thanks. My idea is to handle 1 word first and then handle the whole string. But it seems that regex can't differentiate between words or sentences, it only cares about the whole string. I think will stick with the answer on the other topic then. –  Mar 18 '22 at 15:21

0 Answers0