101

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,738
  • 21
  • 105
  • 131
ion
  • 1,141
  • 3
  • 9
  • 9

8 Answers8

140
[ -~]

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,738
  • 21
  • 105
  • 131
luk3thomas
  • 2,512
  • 1
  • 18
  • 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
114

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

xxx[\x00-\x7F]+xxx

JavaScript example:

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

re.test('xxxabcxxx')
// true

re.test('xxx☃☃☃xxx')
// false
Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
36

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
catwalk
  • 6,340
  • 25
  • 16
11

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

xxx[\x00-\x7F]+xxx
Gumbo
  • 643,351
  • 109
  • 780
  • 844
8

Accepts / Matches only ASCII characters

/^[\x00-\x7F]*$/
Vaibhav Gaikwad
  • 811
  • 2
  • 12
  • 21
2

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
  • 811,555
  • 193
  • 1,581
  • 1,452
0

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

xxx.+xxx
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RoToRa
  • 37,635
  • 12
  • 69
  • 105
-1

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

xxx.+xxx
m_vitaly
  • 11,856
  • 5
  • 47
  • 63