0

I would like to match String that contains letters and numbers WITHOUT space

I tried ^[a-zA-Z+d*]*$ but it matches String that have only letters

This is what it should do :

Nope
Nope 2
MatchPlease123  

If you want to try in live: http://rubular.com/r/pFMkk9ATc0

Thank you

Xavier C.
  • 65
  • 8

2 Answers2

3

You may use

/^(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[a-zA-Z0-9]*$/

See the regex demo (a bit modified since the input is a multiline string).

Details:

  • ^ - start of a string
  • (?=[^a-zA-Z]*[a-zA-Z]) - a positive lookahead requiring that there must be at least one ASCII letter after any 0+ chars other than ASCII letters
  • (?=[^0-9]*[0-9]) - a positive lookahead requiring that there must be at least one ASCII digit after any 0+ chars other than ASCII digits
  • [a-zA-Z0-9]* - 0+ ASCII letters or digits
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Awesome ! Thank you, such a long regex for a little verification lol | I will use it in C# (ASP.NET) but thanks for the details, I've never tried Ruby – Xavier C. May 22 '17 at 14:06
  • In C#, why use Rubular then? It only supports Onigmo regex library. – Wiktor Stribiżew May 22 '17 at 14:08
  • @XavierC.: You will also have to use `[0-9]` instead of `\d` or use a `RegexOptions.ECMAScript` flag. I removed all the tips related to Ruby and adjusted to work with ASCII chars only even in .NET regex with default modifiers. – Wiktor Stribiżew May 22 '17 at 14:10
  • Wiktor ah damn, I saw it on an old Stackoverflow topic, I thought it was a tool to test regex online! (because in C# I have to recompile for every edit, which is super long for debugging) |Edit : ok thanks for the tips – Xavier C. May 22 '17 at 14:10
  • @XavierC. There are a lot of regex testers in the Internet. Those that you may use for testing .NET regexes are RegexStorm.net and RegexHero.net. The best free tool for .NET regex is Ultrapico Expresso (IMHO) (no affiliation with the sites or the tool). – Wiktor Stribiżew May 22 '17 at 14:14
  • 1
    @Rahul: I think you already understand that. You may remove the comment. – Wiktor Stribiżew May 22 '17 at 14:18
2

I think this simplest one will also be helpful

Regex demo

Regex: ^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]+$

1. ^ start of string.

2. (?=.*\d) positive look ahead for digit.

3. (?=.*[a-zA-Z]) positive look alphabets.

4. [a-zA-Z\d]+ match all digits A-Z and a-z

5. $ end of string.

Community
  • 1
  • 1
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • It will also match if there are **only** digits. You will need lookahead for both letters and digits as in Wiktor's answer. – Rahul May 22 '17 at 14:15
  • This is almost same as mine. Note that [`\d` in .NET regex matches more than just `[0-9]` by default](https://stackoverflow.com/a/16621778/3832970). Unless you compile the regex with `RegexOptions.ECMAScript` option. – Wiktor Stribiżew May 22 '17 at 14:17
  • @WiktorStribiżew but i haven't copied for sir believe me. – Sahil Gulati May 22 '17 at 14:18
  • @SahilGulati: I understand your understanding evolved with time, so I understand :) – Wiktor Stribiżew May 22 '17 at 14:19
  • @SahilGulati: _but i haven't copied_ . It happens more often that solutions are similar or same in Regex. Hence advised not to post same solution if anyone else has posted before. – Rahul May 22 '17 at 14:20