nogood
nogood

Reputation: 1320

What is the Regex pattern "words with numbers in them" but not a number itself

How does a regex look like for

Input: Rood Li-Ion 12 G6

Match: "Rood" "Li-Ion" "G6"

1. I tried \b[\w-]+\b /g

But that matches the "12" also!

2.I tried

/([0-9]+)?[a-zA-Zê]/

But that didn't match G6.

I want all words even if they have a number in them but I dont want only numbers to match. How is this possible. Whitespace also shall not be part of the match.

"Rood Li-Ion 12 G6" shall become 3 strings of "Rood","Li-Ion","G6"

Upvotes: 2

Views: 44

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626896

You can use

(?<!\S)(?!\d+(?!\S))\w+(?:-\w+)*(?!\S)

See the regex demo. It matches strings between whitespaces or start/end of string, and only when this non-whitespace chunk is not a digit only chunk.

Also, it won't match a streak of hyphens as your original regex.

Details

  • (?<!\S) - a left whitespace boundary
  • (?!\d+(?!\S)) - no one or more digits immediately to the right capped with whitespace or end of string is allowed
  • \w+(?:-\w+)* - one or more word chars followed with zero or more repetitions of - and one or more word chars
  • (?!\S) - a right whitespace boundary

Upvotes: 1

sp00m
sp00m

Reputation: 48817

This should suit your needs:

\b[\w-]*[a-zA-Z][\w-]*\b

Upvotes: 1

Related Questions