tags:

views:

53

answers:

4

I need a regex to not match tab, carriage return and square brackets. (C#)

+2  A: 

Try:

[^\][\t\r]
  • [] - char class
  • ^ - negation of char class.
  • \] - escape ] as ] is a meta char inside a char class
  • [ - need not escape [ as inside [] its not a meta char
  • \t - tab
  • \r - return carriage
codaddict
I think you are missing `\n` in symbol class
Ivan Nevostruev
OP doesn't mention new-lines.
Douglas Leeder
\n is newline, he only asked for carriage return which is \r
fuzzy lollipop
I hope OP author knows what he's asking for
Ivan Nevostruev
A: 

should be:

[^\t\r\[\]]

or for the whole string:

^[^\t\r\[\]]*$
Kobi
You need not escape [ inside a char class.
codaddict
Well, true. But in this case it might be less confusing.
Kobi
Agreed: it's ugly either way, but it's a little less confusing with the backslash. Besides, in some flavors you *do* have to escape the `[`.
Alan Moore
A: 
[\w]+

Will match any word character (alphanumeric & underscore).

Marcos Placona
And what about all the other characters that are not tabs, carriage-returns or square brackets?
Alan Moore
A: 
#!/usr/bin/perl

use strict; use warnings;

my ($s) = @ARGV;

if ( $s =~ /^[^\r\t\[\]]*\z/ ) {
    print "$s contains no carriage returns, tabs or square brackets\n";
}
Sinan Ünür