Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Find words that do not contain certain characters

Last post 03-07-2011, 2:25 PM by MRAB. 12 replies.
Sort Posts: Previous Next
  •  03-03-2011, 9:00 AM 78692

    Find words that do not contain certain characters

    I have a list of words and I need to fine the ones that do not contain certain characters (or only contain other certain characters, whichever is easier).  I have done this a couple of years ago, but need to do it again and my memory fails me.
  •  03-03-2011, 9:30 AM 78693 in reply to 78692

    Re: Find words that do not contain certain characters

    your question is too generic. Pls read our PostingGuidelines and provide more details on what you are trying to accomplish.
  •  03-03-2011, 12:21 PM 78695 in reply to 78693

    Re: Find words that do not contain certain characters

    Sample list of words:

    golf

    fort

    flag

    frog

     I need a regex to find the words that do not contain a or g.  This should match only fort.

  •  03-03-2011, 12:22 PM 78696 in reply to 78695

    Re: Find words that do not contain certain characters

    To be specific, I have a dictionary in a mysql db and want to get a list of words that can be generated by only using the top row of keys (qwertyuiop).
  •  03-03-2011, 1:26 PM 78697 in reply to 78695

    Re: Find words that do not contain certain characters

    if you are matching the words separately , as individual string, this shoud do it:

    ^[^ag]+$

  •  03-03-2011, 4:48 PM 78701 in reply to 78697

    Re: Find words that do not contain certain characters

    Great, that works.

     

    Here is another one.  is possible to do a match on a word to see if it follows a pattern like consonant, vowel, consoant like banana, or coke?

  •  03-03-2011, 5:15 PM 78702 in reply to 78701

    Re: Find words that do not contain certain characters

    ^([bcdefgnm][aeoue])+$

    enhance the first container [] to include all consonants.

  •  03-03-2011, 9:10 PM 78705 in reply to 78702

    Re: Find words that do not contain certain characters

    Fantastic!

     

    Here is the results of these regex expressions

     

    http://www.patricklewis.net/games/dropwords/DictCheck.php

     

  •  03-04-2011, 9:07 AM 78715 in reply to 78705

    Re: Find words that do not contain certain characters

    hey I am also in a similar situation.

    I need to validate user input. This is how I do.

    /^[\w\._-]{5,10}$/

    This allows a periods, underscores, hypens, any alphanumerics and its length should be in range of 5-10

    Now I need to blacklist certain key words like 'drop' 'delete' insert' etc. which should not be allowed

    /^[\w\._-]{5,10}$/

    This allows a periods, underscores, hypens, any alphanumerics and its length should be in range of 5-10

    Now I need to blacklist certain key words like 'drop' 'delete' insert' etc. which should not be allowed

    How do I add this extra condition checking to the above expression??

    I tried adding this ^[^drop|delete|insert]+$ to the above expression which results in

    /^[\w\._-]{5,10}^[^drop|delete|insert]+$/       but this doesn't work

    Sergei Z, plz help me.

     Thanks in advance.

  •  03-04-2011, 10:11 AM 78716 in reply to 78715

    Re: Find words that do not contain certain characters

    pls specify your platform/programming language/tool. Regex syntax/solution, much like any other type of code, depends on these.

     

  •  03-04-2011, 1:21 PM 78720 in reply to 78715

    Re: Find words that do not contain certain characters

    The [...] defines a set of characters, not a set of words. Probably what you want is a negative lookahead:

        /^(?!drop|delete|insert)[\w\._-]{5,10}$/

    Actually, there's no need to exclude "drop", because you're restricting the number of characters to between 5 and 10, and "drop" has only 4.

    By the way, judging from the list of words you want to blacklist, it looks like your intention may be to use regex to stop SQL injection attacks. If that's the case, your approach is wrong. Any decent SQL implementation will have a way to put values safely into an SQL command or query.

  •  03-06-2011, 10:56 PM 78852 in reply to 78720

    Re: Find words that do not contain certain characters

    Thanks for ur input guys.

    @MRAB: You got it right. I am here trying to avoid sql injection. I know that parameterized queries are safe n secure. But I am just trying to bypass blacklisting approach.

    And comin to the point, the regexp which you suggested works but it has some problems.

    deleteadmi -> matches the regexp

    adelete -> Doesn't match

    To summarize, only if the blacklisted words are at the beggining of the string, it matches. If the string starts with any other character followed by blacklisted word, it doesn't match.

    So what I did is this :    /^[\w\._-]+^(?!drop|delete|insert)[\w\._-]{5,10}$/

    But this is also not working. Any idea?

     

  •  03-07-2011, 2:25 PM 78872 in reply to 78852

    Re: Find words that do not contain certain characters

    The "^" matches only at the start of the string (or line if you're using multiline mode), so:

        /^[\w\._-]+^(?!drop|delete|insert)[\w\._-]{5,10}$/

    is anchored at the start of the string, then matches a "word" (at least one character), then tries to match the string of the string again, but it's already at least one character beyond it, so no match.

View as RSS news feed in XML