nas
nas

Reputation: 2417

Regex to match only few letters and numbers

How can I write a regex that matches letters ('R', 'L'), numbers and first character is always letter. E.G.

I want regex to accept string like "R12L", "L1" that start with either 'R' or 'L' only.

Upvotes: 0

Views: 1557

Answers (1)

Tamas Rev
Tamas Rev

Reputation: 7166

I believe you want to match words that:

  • Start with any letter
  • contain numbers, 'R' and 'L'

Here is: \b[a-zA-Z][0-9RL]*\b

In case the first letter must be either 'R' or 'L', then this will be better:

`\b[RL][0-9RL]*\b`

Explanation:

  • \b is a word boundary, a zero length match
  • [RL] is a character class, it matches either R or L
  • [0-9] is a range within the character class, it matches anything between 0 and 9.

You can play with this demo.

Upvotes: 2

Related Questions