32

I have this string:

" abalbal asldad  23 sadaskld 3123 adasdas "

How to match only the words, without numbers.. with " \D* " I can match only the first two, without others..

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
ROZZ
  • 1,334
  • 3
  • 20
  • 36

4 Answers4

61

You can use this regex:

/\b[^\d\W]+\b/g

to match all words with no digits.

RegEx Demo

[^\d\W] will match any non-digit and (non-non-word) i.e. a word character.

anubhava
  • 761,203
  • 64
  • 569
  • 643
8

I'd use this one:

/\b([a-z]+)\b/gi

or, to be unicode compatible

/(\p{L}+)/g
Toto
  • 89,455
  • 62
  • 89
  • 125
2

If the task is to match words that only consist of letters, you can use the following solutions:

  • : r'\b[^\W\d_]+\b' (see demo)
  • : /\b\p{Alphabetic}+\b/gu (see demo) NOTE: requires ECMAScript 2018+ support.
  • : '~\b\p{L}+\b~u' (see demo)
  • : /\b\p{L}+\b/ (see demo)
  • : @"\b\p{L}+\b" (see demo)
  • : "\b\p{L}+\b" (see demo)
  • : R"(\b[[:alpha:]]+\b)" (see demo) NOTE: Only ASCII letters are considered word chars.
  • : "(*UCP)\\b\\p{L}+\\b" for base R function with perl=TRUE (see demo) or just "\\b\\p{L}+\\b" for stringr regex functions
  • : "%f[%a]%a+%f[%A]" (see demo)NOTE: Only ASCII letters are considered word chars.
  • : sed -E 's/\b[[:alpha:]]+\b/DELETED/g' file (see demo)
  • : grep -oE '\b[[:alpha:]]+\b' file (see demo)
  • : set pattern {\y[[:alpha:]]+\y} (see demo)

Note that most PCRE-based or PCRE-originated regex flavors use \b to match leading/trailing word boundaries. PostgreSQL and Tcl use \y instead of \b. There are other types of word boundaries, like \m (Tcl, PostgreSQL), or \< (Vim, GNU sed or grep, base R), or [[:<:]] (MacOS FreeBSD grep/sed, MySQL up tp v6, base R with perl=TRUE, PCRE in general) to match a leading word boundary and \M, or \>, or [[:>:]] accordingly to match closing, or trailing, word boundary.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • May you please add regex for `perl`? – Eugen Konkov Nov 28 '22 at 11:11
  • 1
    @EugenKonkov Perl pattern to match a letter-only word is `\b\p{L}+\b`. You may use it in the matching or replacing context, but the final solution will depend on what you actually are doing. Also, see [How can I find all matches to a regular expression in Perl?](https://stackoverflow.com/q/1723440/3832970) – Wiktor Stribiżew Nov 28 '22 at 11:14
0

This expression worked for me:

/[a-zA-Z]+'?[a-zA-Z]+/g

Not only it matches all the words and ignores the numbers, it also gets words like "he's", "we're", "what's", "Bruce's".

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61