The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-30-2008
iengca iengca is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 5
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.
  #2 (permalink)  
Old 01-31-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
What language or shell script are we talking about here? Can we also see some code? Maybe your usage is wrong.
  #3 (permalink)  
Old 01-31-2008
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
There's no mystery, '[^ ]' means match any single character that is not a space.
  #4 (permalink)  
Old 02-01-2008
iengca iengca is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 5
Quote:
Originally Posted by Ygor View Post
There's no mystery, '[^ ]' means match any single character that is not a space.
I thought the same as you, "match any single char that is not space". But we are wrong.

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.
  #5 (permalink)  
Old 02-02-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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.
  #6 (permalink)  
Old 02-07-2008
iengca iengca is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 5
Isn't Regular Expression generic for all languages? I am under the impression that Regular Expression use the same syntax even across platform, ie. same for unix, windows or linux. I am using it in webMethods here.
  #7 (permalink)  
Old 06-17-2008
sonam sonam is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 2
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.
Closed Thread

Bookmarks

Tags
linux, perl, perl regex, regex, regular expressions, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0