![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need a regular expression | tony3101 | Shell Programming and Scripting | 4 | 06-05-2008 04:13 AM |
| regular expression and awk | nickg | UNIX for Dummies Questions & Answers | 2 | 08-16-2007 06:23 PM |
| regular expression...help!! | andy2000 | UNIX for Dummies Questions & Answers | 6 | 07-18-2007 06:10 PM |
| help in regular expression | Rakesh Ranjan | High Level Programming | 5 | 10-25-2006 02:00 PM |
| Regular Expression + Aritmetical Expression | Z0mby | Shell Programming and Scripting | 2 | 05-21-2002 11:59 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
regular expression [^ ]
Hi,
Could anybody explain why [^ ] will evaluate to true for a string that is not null, not empty string, and has at least one non-space char? My understanding is that ^ means exclude all chars inside []. So I thought it should mean anything except space. This seems a big mystery to me. |
|
||||
|
Quote:
It matches any string that is not null, not empty string and not only space. But I don't understand how that could be. I need someone to explain it. |
|
||||
|
Hard to answer when you do not define the language you are using, there is no examples of how you are using it or the data you are using. [^ ] is a negated space, not to be confused with other non-visible characters like tabs or newlines or other stuff I probably am not aware of: Code:
@t = ('m', "\t", '1', " ", "\n");
for my $i (0..$#t) {
if ($t[$i] =~ /[^ ]/) {
print qq{Number $i is true "$t[$i]"\n};
}
}
the above is true for all except " ", which is a space. \t (tab) and \n (newline) are not included in [^ ] but they would be in [^\s]. Code:
@t = ('m', "\t", '1', " ", "\n");
for my $i (0..$#t) {
if ($t[$i] =~ /[^\s]/) {
print qq{Number $i is true "$t[$i]"\n};
}
}
Of course the above is perl. But since you have not mentioned (after being asked) what language you are using, maybe I just wasted my time. |
|
||||
|
Actually regular expression support is not uniform in all languages. Perl compatible regular expressions (also called PCRE) are a very common set of regular expressions compatible with how Perl supports regular expressions. Regular expression support in the webMethods 'Flow' language is not explicitly stated as PCRE according to their documentation (but may be). The original poster may have been asking about this statement from an article here: (because I hit this forum when researching the same topic) -- Regular Expressions for Integration Server wMUsers: For webMethods Professionals -- Knowledge Base | Regular Expressions for Integration Server -- /[^ ]/ -- [matches a variable that is] is not null, is not empty, and contains at least one non-space character. My question was also similar - how come this does _not_ match the empty string, or null? I think the answer is a square bracket pair ('[...]') must match some character, and the '^ ' just excludes the space character from matching. Is this correct? I modified the Perl code helpfully posted earlier, by adding an empty string ("") as the last case: Code:
#!/usr/bin/perl
@t = ('m', "\t", '1', " ", "\n","");
for my $i (0..$#t) {
if ($t[$i] =~ /[^ ]/) {
print qq{Number $i is true "$t[$i]"\n};
} else {
print qq{Number $i is NOT true "$t[$i]"\n};
}
}
Running this, I get these results: Code:
Number 0 is true "m" Number 1 is true " " Number 2 is true "1" Number 3 is NOT true " " Number 4 is true " " Number 5 is NOT true "" So does result #5 confirm my understanding above --- the square bracket pair ('[...]') _must_ match some character, and the '^ ' just excludes the space character from matching. |
![]() |
| Bookmarks |
| Tags |
| linux, perl, perl regex, regex, regular expressions, solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|