in reply to How to detect non printable characters and non white space characters? [RESOLVED]
First, /g means "global", which means search all the matching results, don't stop at the first one (either one result at a time, or all of them at once). This technically doesn't change anything, but you just need to find that there is one non-printable character to keep the string. So the first regex could just be /[^[:print:]]/.
The straightforward solution to your problem is to use an and rather than an or in your condition. You want a string that (only contains valid printable chars) AND (only contains valid non-space chars) => (does not contain non-printables) AND (does not contain spaces). De Morgan's laws might also be a good read on how to negate a condition with ORs and ANDs.
However, I think you can obtain something easier to read with unless (which is an "if not") and next which will stop processing the current element in a loop, and go on to the next.
# Delete the value and try the next one unless it has a non-printable, + or a space delete $$hoh_ref{$key}{$value} and next unless /[^[:print:]]/ or /\s/; while ($$hoh_ref{$key}{$value} =~ /([^[:print:]])/g) { print "Unprintable !\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to detect non printable characters and non white space characters?
by thanos1983 (Parson) on Feb 17, 2017 at 11:09 UTC |