czupe
czupe

Reputation: 4919

REGEXP valid on not alphanumeric and not space, '-' characters

I want to find only non alphanumeric characters and only without space without '-'...

But this is not working:

REGEXP '^[a-zA-Z0-9/-:space:]*'

This regexp is valid on these data:

 'Test Cinema'
  'AMERICANII AU TALENT - SEZONUL 5, EPISODUL 12'

I dont want to find these characters, i want to find fields like this:

'Test Item;?!'
'LOCAL KOMBAT "FIERBE BULGARIA"'

How can i write a regexp which is only valid on other characters.

Upvotes: 3

Views: 3848

Answers (3)

Ghost
Ghost

Reputation: 2226

It would be useful to have a list of things the test should match and should not match clearly defined, as it is kind of have to guess

Try

1. ^.*?[^A-Za-z0-9 -]+[A-Za-z0-9 -]*$
2. ^.*?[^A-Za-z0-9, -]+[A-Za-z0-9, -]*$

1 matches ( the first one because of the comma)

AMERICANII AU TALENT - SEZONUL 5, EPISODUL 12
Test Item;?!
LOCAL KOMBAT "FIERBE BULGARIA"

but does not match Test Cinema

2 matches

Test Item;?!
LOCAL KOMBAT "FIERBE BULGARIA" 

But does not match

Test Cinema
AMERICANII AU TALENT - SEZONUL 5, EPISODUL 12

You can play with the second one here I had to negate the newline to make it work for the sample, but just add one test per line to see whether or not it matches

Upvotes: 1

Kendall Frey
Kendall Frey

Reputation: 44316

[^a-zA-Z0-9 -]*

That would be what you want, I think.

By placing the - at the end of the [], the regex knows you mean a literal -, and you don't need to escape it with \.

Edit: To match the whole string, use this:

^[^a-zA-Z0-9 -]*$

^ and $ are used to anchor the regex to the beginning and end of the string.

Upvotes: 4

pizen
pizen

Reputation: 474

The ^ needs to be inside the brackets for it to be a negation. When placed at the beginning of the regex ^ means that it should be matched at the beginning of the string.

Try this to match all non-empty, non-alphanumeric (including space and -) strings:

^[^a-zA-Z0-9 -]+$

Upvotes: 6

Related Questions