tags:

views:

161

answers:

6

I need to remove all characters from the given string except for several which should left. How to do that with regexp?

Simple test: characters[1, a, *] shouldn't be removed, all other should from string "asdf123**".

+4  A: 

When used inside [ and ] the ^ (caret) is the not operator.

It is used like this:

"[^abc]"

That will match any character except for a b or c.

jjnguy
When used *as the first character* inside...
dash-tom-bang
+1  A: 

There's a negated character class, which might work for this instance. You define one by putting ^ at the beginning of the class, such as:

[^1a\*]

for your specific case.

eldarerathis
you ned to escape the *, should be \\*
Cfreak
Actually you don't (in regex dialects that I'm aware of, anyway), but you can. I'll add it to be explicit, though.
eldarerathis
@Cfreak: no you don't (see above)
Mike Axiak
+10  A: 

There is: ^ in a set.

You should be able to do something like:

text = text.replaceAll("[^1a*]", "");

Full sample:

public class Test
{
    public static void main(String[] args)
    {
        String input = "asdf123**";
        String output = input.replaceAll("[^1a*]", "");
        System.out.println(output); // Prints a1**
    }
}
Jon Skeet
You don't need to escape the `*` in a character class.
KennyTM
@KennyTM: Ooh, I didn't know that. Cheers, adjusting now...
Jon Skeet
You also don't need to escape a hyphen if it's at the end or the beginning of a character class. So [^*.-] represents everything that's not an asterisk, a period, or a hyphen. :)
Mike Axiak
Thanks! I was confused a little that the same sign is used to match the beginning of the string.
Roman
A: 

In a character class the ^ is not. So

[^1a\*] would match all characters except those.

Cfreak
A: 

You want to match against all characters except: [asdf123*], use ^

A: 

There's no "not" operator in Java regular expressions like there is in Perl.

Steve Emmerson
Are you talking about Perl's inverted regex match operator `!~`? That's not really relevant, given that the OP is doing a substitution, not a match. And if he *were* trying to match the characters rather than replace them, he would need to do a *positive* match for a regex that excludes those characters, as the other responders pointed out. For example, in Perl he might do this: `my @result = $s =~ m/[^1a*]+/g;`
Alan Moore