dsignr
dsignr

Reputation: 2354

Regular Expressions - Match all alphanumeric characters except individual numbers

I would like to create a RegEx to match only english alphanumeric characters but ignore (or discard) isolated numbers in Ruby (and if possible in JS too).

Examples:

1) I would like the following to be matched:

But not:

The above is exactly what I would want.

Edit: I deleted the second sub-question. Just focus on the first one, thank you very much for your answers!!

Sorry, my regex skills aren't that great (hence this question!)

Thank you.

Upvotes: 0

Views: 1718

Answers (4)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

You can try the following expression (works both in Ruby and Javascript):

^(?!^\d+$)[[:alnum:]]+$

This first ensures the string is not just digits by using a negative look ahead (?!^[0-9]+$), then it matches one or more alphanumeric character, Unicode characters are supported which means this works with French letters too.

EDIT: If you only want English alphabet:

^(?!^\d+$)\w+$

Rubular Demo

Upvotes: 1

user557597
user557597

Reputation:

Maybe this ^(?![0-9]+$)[a-zA-Z0-9\x80-\xa5]+$

Edit - fixed cut&paste error and added Extended character range \x80-\xa5
which includes the accent chars (depending on locale set, the figures may be different)

Upvotes: 0

talemyn
talemyn

Reputation: 7950

I'm pretty sure that you can't do what you want to do with one regex. A single alpha character, anywhere in a group of numbers, will make it a valid match, and there is no way to represent that in regex, because what you are really saying is something along the lines of "a letter is required at the front of this word, but only if there isn't a letter in the middle or at the end", and regex won't do that.

Your best bet is to do two passes:

  1. one that matches your alphanumeric, plus special "French" characters (pattern: TBD, based on what special characters you want to accept), and
  2. one that matches numbers only (pattern: would include [0-9]+ . . . need more information about the specific situation to give you a final, complete regex)

The values that you want in the end would need to pass the first regex and fail the second one.

Also . . .

To give you a better answer, we'll need to know a couple of things:

  • Are you testing that an entire string matches the pattern?
  • Are you trying to capture a single instance of the pattern in a bigger string?
  • Are you trying to capture all of the instances of the pattern in a bigger string?

The answers to those questions have a big impact on the final regex pattern that you will need.

And, finally . . .

A note on the "French" characters . . . you need to be very specific about which special characters are acceptable and which aren't. There are three main approaches to special character matching in regex: groups, additive, and subtractive

  • groups - these are characters that represent a preset group of characters in the version of regex that you are using. For example, \s matches all whitespaces
  • additive - this is the process of listing out each acceptable character (or range of characters) in your regex. This is better when you have a small group of acceptable characters
  • subtractive - this is the process of listing out each UNacceptable character (or range of characters) in your regex. This is better when you have a large group of acceptable characters

If you can clear up some of these questions, we should be able to give you a better answer.

Upvotes: 1

sawa
sawa

Reputation: 168101

For any Latin letters:

/(?=.*\p{Alpha})\p{Alnum}+/

Upvotes: 1

Related Questions