Jump to content

RegEx: Howto *INVERT* a class of characters?


Recommended Posts

Hi.

I want to catch the part of a string from the start of the string with a stringregexp().

This code pasted by Xenobiologist was my start.

I didn't want to define a class for valid chars by specifying all valid chars. Instead of that approach I wanted to invert the "not allowed" chars:

Isn't (abc) a character class matching any "a", "b", "c", and isn't a "^" the 'ClassInverter', so that (^abc) would match any Char BUT "a", "b", "c"?

muttley regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Not sure if you are looking for an array or boolean result...

Array:

$string = "abcdefghijklmnopqrstuvwxyz"
$result = StringRegExp($string, "[^abc]", 3)
For $X = 0 to Ubound($result)-1
    ConsoleWrite("["&$X&"]: " & $result[$X] & @CRLF)
Next
Link to comment
Share on other sites

Regardless of what output you want, classes go inside [ ] square brackets, and ^ will cause it to match anything not in the set ONLY if it is the first character after the opening bracket [

http://www.regular-expressions.info/reference.html

Bookmark that page... I use it regularly for reference.

Regards,Josh

Link to comment
Share on other sites

Regardless of what output you want, classes go inside [ ] square brackets,

Yes, that's a typo. In my code I used []

... and ^ will cause it to match anything not in the set ONLY if it is the first character after the opening bracket [

http://www.regular-expressions.info/reference.html

So [^abc] should match any characters BUT "a", "b", "c", did I get that correctly?

Thanks, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Yes, that's a typo. In my code I used []

So [^abc] should match any characters BUT "a", "b", "c", did I get that correctly?

Thanks, Rudi.

Correct. But the method I posted will just return an array of all characters that aren't a, b, or c. If you want to strip these characters and return just a string you will need StringRegExpReplace.
Link to comment
Share on other sites

... you will need StringRegExpReplace.

Why StringRegExpREPLACE? I want to catch the part of a string that matches this class from the beginning of the lines muttley

I'm on site at that customer next week again, then I'll look up what I messed up in my stringregexp().

IIRC I tried to use something like "^[^\\/:\*\?\"\<\>\|]+" :) I expected this to catch from the beginning of the string the substring upto the position with the first invalid character (exclusive). (matchmode = 3)

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...