Re: Regexp non-alphanumerics to spaces



"John W. Krahn" schreef:
Dr.Ruud:
John W. Krahn:
Rob Dixon:
Grant:

Hello, can you guys show me how to convert each non-alphanumeric
character in a string to a space, and then convert any number of
consecutive spaces in the string to a single space?

$text =~ s/[^[:alnum:]]/ /g;
$text =~ s/\s+/ /g;

$text =~ s/[[:^alnum:]\s]+/ /g;

Isn't \s already part of [[:^alnum:]]?

Oops, yes, I was wrong. :-)

Not really, the \s did no harm, it was just extra.


The space I am always concerned about is 0xA0:

$ perl -wle '$_ = "\xA0"; /\s/ and print "s"'

$ perl -wle '$_ = "\xA0\x{100}"; chop; /\s/ and print "s"'
s

This is because in Perl (5.8?) the first string is a Latin1 string, and
the second a utf8 string.

--
Affijn, Ruud

"Gewoon is een tijger."

.



Relevant Pages

  • Re: Top kernel oopses/warnings this week
    ... terminate the oops printing with a defined string/uuid ... Printing the boot UUID ... as part of the end string resolves this ambiguity. ... void srandom32; ...
    (Linux-Kernel)
  • Re: How can I know if a string is alphanumeric?
    ... >> How can I know if a string only has alfanumeric chars? ... > By feeding it to is_numeric: ... Oops, sorry, didn't read the question carefully... ...
    (comp.lang.php)
  • Re: Code not working correctly
    ... Oops, I thought your reply was the reason it wasn't working and I answered ... section after the "&" sign in the strTableName statement, ... Dim strTableName As String, strSQL As String ...
    (microsoft.public.access.modulesdaovba)
  • Re: Regexp non-alphanumerics to spaces
    ... consecutive spaces in the string to a single space? ... Oops, yes, I was wrong. ...
    (perl.beginners)
  • Re: Variables in the HTML part of a web form...
    ... Oops! ... I didn't notice the DatabaseColumn reference in the DataBind.Eval ... >a constant that contains a string value, that string value is what it ... >> MY ASP.Net tutorials ...
    (microsoft.public.dotnet.framework.aspnet)