Regexp problem

K

kj

I want to remove from a (variable) string any character whose ascii
code is greater than 127, along with any whitespace preceding it.
The only regexp I can think of to do this with is the abominable:

/\s*(.)/ord($1) > 127 ? '' : $1/eg

Is there a better way?

Thanks!

kj
 
B

Ben Morrow

kj said:
I want to remove from a (variable) string any character whose ascii
code is greater than 127, along with any whitespace preceding it.
The only regexp I can think of to do this with is the abominable:

/\s*(.)/ord($1) > 127 ? '' : $1/eg

Is there a better way?

There *are* no characters whose ASCII codes are greater than 127, so
this will do what you want:

s/\s*[^[:ascii:]]//g;

Ben
 
J

John W. Krahn

kj said:
I want to remove from a (variable) string any character whose ascii
code is greater than 127,

Anything greater than 127 is not ASCII.
along with any whitespace preceding it.
The only regexp I can think of to do this with is the abominable:

/\s*(.)/ord($1) > 127 ? '' : $1/eg

Is there a better way?

s/\s*[^[:ascii:]]+//g;


John
 
K

kj

I want to remove from a (variable) string any character whose ascii
code is greater than 127, along with any whitespace preceding it.
The only regexp I can think of to do this with is the abominable:
/\s*(.)/ord($1) > 127 ? '' : $1/eg

Oops. Make that s/(\s*(\S))/ord($2) > 127 ? '' : $2/eg

It's just as bad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top