Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex any ASCII character

Tags:

regex

ascii

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.

like image 209
ion Avatar asked Jul 08 '10 11:07

ion


People also ask

Is ASCII an regex?

Regex makes it easy to match any specific ASCII character or a range of characters. A regular expression that matches ASCII characters consists of an escaped string \x00 where 00 can be any hexadecimal ASCII character code from 00 to FF.

What is \\ p ASCII?

Description. The character class \p{ASCII} matches any ascii character.

How do you denote special characters in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What is a non ASCII character?

Non-ASCII characters are those that are not encoded in ASCII, such as Unicode, EBCDIC, etc. ASCII is limited to 128 characters and was initially developed for the English language.


Video Answer


2 Answers

[ -~] 

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

So your implementation would be:

xxx[ -~]+xxx 
like image 122
luk3thomas Avatar answered Nov 16 '22 03:11

luk3thomas


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 
like image 45
Matthew Flaschen Avatar answered Nov 16 '22 04:11

Matthew Flaschen