Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to find strings that don't contain certain characters

    I've looked through the forum and haven't seen this topic before so I was curious if anyone knew how to do this. I'm trying to write commands that will list any observations that don't contain a given string anywhere in their answer. Essentially it would be the opposite of a regular expression, so that it would exclude these observations rather than include them. For example, I'd want to exclude all observations that have a negative answer, so that any string that includes "NO" (whether as "NO" by itself or "NO ONE", etc.) anywhere in their answer would be excluded from the list. Is there a way to do this?
    Many thanks in advance,
    Ron

  • #2
    If you are talking about string variables, you could do something like

    Code:
    l if !(strmatch(strupper(strvar), "*NO*"))
    In any case, think about using string functions (-help string functions-) instead of regular expressions.

    Best
    Daniel

    Comment


    • #3
      I support Daniel's advice to think in terms of the simpler string functions. Even simpler is

      Code:
       l if !(strpos(strupper(strvar), "NO"))
      as strpos() returns 0 if its second argument is not found.

      Comment


      • #4
        Thanks Daniel and Nick. You're right, I'm working with strings. I'll give that a shot and many thanks again for the help.

        Comment


        • #5
          I've tried it and it worked great. Thanks again!

          Comment

          Working...
          X