POSIX Regular Expressions: Excluding a word in an expression?

12,005

According to http://www.regular-expressions.info/refflavors.html lookaheads and lookbehinds are not in the POSIX flavour.

You may consider thinking in terms of lexing (tokenization) and parsing if your problem is too complex to be represented cleanly as a regex.

Share:
12,005
9codeMan9
Author by

9codeMan9

I am a graduate student in IST with a background in Computer Science and Engineering, Philosophy, and Mathematics. I am currently conducting research in deep architectures for efficient, large-scale learning. My interests include: Artificial Intelligence: Intelligent Agents, Neural Networks, and Knowledge Representation Complex Systems, Artificial Intuition & Artificial Creativity Computer Graphics and Computer Vision How human agents and software agents can work together to solve problems (HCI + AI) Lambda Calculus, Interpreters

Updated on July 30, 2022

Comments

  • 9codeMan9
    9codeMan9 almost 2 years

    I am trying to create a regular expression using POSIX (Extended) Regular Expressions that I can use in my C program code.

    Specifically, I have come up with the following, however, I want to exclude the word "http" within the matched expressions. Upon some searching, it doesn't look like POSIX makes it obvious for catching specific strings. I am using something called a "negative look-a-head" in the below example (i.e. the (?!http:) ). However, I fear that this may only be something available to regular expressions defined in dialects other than POSIX. Is negative lookahead allowed? Is the logical NOT operator allowed in POSIX (i.e. ! )?

    Working regular expression example:

    href|HREF|src[[:space:]]=[[:space:]]\"(?!http:)[^\"]+\"[/]

    If I cannot use negative-lookahead like in other dialects, what can I do to the above regular expression to filter out the specific word "http:"? Ideally, is there any way without inverse logic and ultimately creating a ridiculously long regular expression in the process? (the one I have above is quite long already, I'd rather it not look more confusing if possible)

    [NOTE: I have consulted other related threads in Stack Overflow, but the most relevant ones seem to only ask this question "generically", which means answers given didn't necessarily mean they were POSIX-flavored ==> in another thread or two, I've seen the above (?!insertWordToExcludeHere) negative lookahead, but I fear it's only for PHP.)

    [NOTE 2: I will take any POSIX regular expression phrasings as well, any help would be appreciated. Does anyone have a suggestion on how whatever regular expression that would filter out "http:" would look like and how it could be fit into my current regular expression, replacing the (?!http:)?]

  • 9codeMan9
    9codeMan9 about 11 years
    Well, the above regular expression I have posted is close to what I need, minus the exclusion of the string "http:". Do you have any suggestions on how I get the exclusion of "http" worked into my regular expression using POSIX? In other words, any suggestions for how I can incorporate an expression in POSIX that would filter out "http:" but could put within my current regular expression?
  • Scott Lamb
    Scott Lamb about 11 years
    It's possible by long expressions like ([^h"][^"]+|h[^t"][^+]+|ht[^t"][^t"]+|... but I wouldn't recommend it. I'd second Patashu's recommendation of thinking in terms of lexing and specifically recommend you look for an existing library for parsing HTML. It will get other details right like that the attributes can have single quotes as well as double quotes, something that looks attribute-like may be part of the body text or a comment or a CDATA section, etc.
  • 9codeMan9
    9codeMan9 about 11 years
    While I completely agree with both of you, I'm going to have to have to just come up with a regex for this long form and insert where the negative look ahead currently is in. What are your thoughts on my expression below? It's inspired by a forum post on SO where someone wanted to filter out "tree", but I modified it to filter out "http": ^([^h]|(h[^t])|(ht[^t])|(htt[^p]))*($|(h($|(t($|p$)))))