91

What is the regex to match xxx[any ASCII character here, spaces included]+xxx?

I am trying xxx[(\w)(\W)(\s)]+xxx, but it doesn't seem to work.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
ion
  • 1,041
  • 2
  • 9
  • 9

8 Answers8

111
[ -~]

It was seen here. It matches all ASCII characters from the space to the tilde.

So your implementation would be:

xxx[ -~]+xxx
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
luk3thomas
  • 2,212
  • 1
  • 17
  • 20
  • This is perfect, since the accepted answer did not work with [RegularExpression] attribute in ASP.NET MVC - it gets rendered as Unicode chars and this breaks the validation. – Art Dec 08 '15 at 03:36
  • 4
    Really elegant solution, highly readable and semantically exactly what I was looking for. – machine yearning Apr 28 '16 at 13:49
  • @Art This is not working for me with a `[RegularExpression]` attribute... Did you have to do anything else special for it to work? My attribute is just `` but won't even accept "asdf". – Zack Aug 26 '16 at 18:44
  • 1
    I guess never mind. I just realized that will only match one character, so I had to add the + to make it `[ -~]+` "any ascii character, one or more times" for my usage. – Zack Aug 26 '16 at 18:49
  • 1
    @Zack check out this article I wrote with the fully working example code: http://nimblegecko.com/how-to-validate-a-textfield-for-only-printable-characters-in-aspnet-mvc/ Hope it helps and let me know if you're stuck! – Art Aug 27 '16 at 00:11
  • @Art Yeah it is working, I just didn't think about how I would need the + on the end to make it match a character "one or more" times, and I got confused when it wouldn't accept anything I tried to test with like "qwert", "1234" etc, because they were all longer than one character. – Zack Aug 29 '16 at 13:55
  • Very helpful. I used `[^ -~]` and replace every match by empty string to remove non ASCII characters. – Sebastian Oct 11 '19 at 07:09
  • great answer, see live demo (space excluded) https://regex101.com/r/Xuj0Pf/1 – humble_wolf Sep 26 '21 at 08:39
108

If you really mean any and ASCII (not e.g. all Unicode characters):

xxx[\x00-\x7F]+xxx

JavaScript example:

var re = /xxx[\x00-\x7F]+xxx/;

re.test('xxxabcxxx')
// true

re.test('xxx☃☃☃xxx')
// false
Matthew Flaschen
  • 268,153
  • 48
  • 509
  • 534
32

You can use the [[:ascii:]] class.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
catwalk
  • 6,028
  • 23
  • 16
  • @catwalk You are my hero for today! It wasn't accepting \x00-\x7F for some reason, but it took [:ascii:]. Thanks! – n0nag0n Jul 20 '12 at 22:31
  • 6
    Note that [[:ascii:]] would match any ASCII character, even non-printable characters, whereas [ -~] would match just the printable subset of ASCII. – elolos Sep 02 '14 at 11:58
  • *"character class"*? – Peter Mortensen Aug 07 '18 at 22:04
  • Note: A better name would be "POSIX expression", instead of "class". Be aware that it is not supported in every language, for example JavaScript, does not support it. – Daniel B. Jun 03 '20 at 14:21
11

Since US-ASCII characters are in the byte range of 0x00–0x7F (0–127):

xxx[\x00-\x7F]+xxx
Gumbo
  • 620,600
  • 104
  • 758
  • 828
8

Accepts / Matches only ASCII characters

/^[\x00-\x7F]*$/
Vaibhav Gaikwad
  • 721
  • 1
  • 10
  • 20
3

Try using .+ instead of [(\w)(\W)(\s)]+.

Note that this actually includes more than you need - ASCII only defines the first 128 characters.

Mark Byers
  • 767,688
  • 176
  • 1,542
  • 1,434
0

. stands for any char, so you write your regex like this:

xxx.+xxx
m_vitaly
  • 11,658
  • 5
  • 47
  • 61
-1

Depending on what you mean with "ASCII character" you could simply try:

xxx.+xxx
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
RoToRa
  • 36,594
  • 12
  • 66
  • 103