5

I have this file named test:

http://edge.sharethis.com
http://edge.sharethis.com
https://timetosa.com
http://timetosa.com
https://webtest.es
holahttp.com
timetosa

I want a expression that excludes any line that contains timetosa.

This is not allowed : cat test | grep -v timetosa, because I want to use a pure regular expression in another program.

I know it has to do with ^ but cannot get to the right solution, to affect the ones which not include the string timetosa:

cat test | sed 's/^[timetosa]//g' and cat test | sed 's/^(timetosa)//g' without success.

Could anyone help me ¿?

  • 4
    Why grep -v do not allowed? What programm you want to use? What is the main goal of regex? If you need sed you can use sed '/timetosa/! d' – Costas Feb 10 '15 at 9:57
  • 1
    You can also look at this question asked earlier - stackoverflow.com/questions/406230/… – rahul Feb 10 '15 at 9:58
  • 1
    I am using this regular expression for ZAP proxy, so I cannot use linux commands there hehe – aDoN Feb 10 '15 at 9:59
  • @Costas - I think you might have that sed backwards... – mikeserv Feb 10 '15 at 10:09
  • timetosa as a regexp would match anything that contains timetosa unless your regexps are implicitly anchored (as with grep -x) in which case you'll want .*timetosa.* (beware of the effect of locales though as . could fail to match bytes that are not part of valid characters). – Stéphane Chazelas Feb 10 '15 at 10:11
2
sed '/timetosa/d' <test

...will do it. Alternatively:

sed -n '/timetosa/!p' <test

Still, though (whether it's allowed or not):

grep -v timetosa <test

...is going to be the most performant solution of the three - and probably by a significant margin.

Thanks to @Sparhawk, I found my way to the zaproxy documentation. Based on this:

  • URL regexs
    • In the Include in *, Exclude from * panels and the Logged in/out indicators of the Authentication panel, you can enter regular expressions to define excluded URLs.

...and the following, I would guess you're trying to filter Contexts? According to the docs, you can do both include and exclude lists:

  • Exclude from context
    • This allows you to manage the URLs which will be excluded from the context.
    • You only need to specify regexs for URLs that you do not want to include but which match one or more of the include regexes.

So you can exclude some of your previous inclusions.

Still, though, it may be the first bit of this is not entirely irrelevant - the docs also mention this in the Add-ons section:

  • Invoke Applications

    • Other applications can be invoked passing in context information, such as the URL of the message selected.
    • So, for example, nmap could be invoked passing the site which you want it to scan.

    • Applications are configured using the Options Applications screen.

  • 2
    I think they are asking for a generic regex, not a specific implementation in grep/sed, since they are using it in "ZAP proxy", whatever that is. – Sparhawk Feb 10 '15 at 10:11
  • @Sparhawk - Ok - I'm gonna delete this - but where do you get the ZAP proxy thing? I don't see that anywhere... All I see is the cat | sed examples... – mikeserv Feb 10 '15 at 10:15
  • 1
    In the comments to the question? Third one down. – Sparhawk Feb 10 '15 at 10:16
  • No worries, and nice investigative work! – Sparhawk Feb 10 '15 at 12:04
0

Regular languages are closed under complementation, so for every regular expression, there exists a regular expression that matches exactly the inputs that the original regexp doesn't match.

However, in the worst case, the smallest regexp that matches the complement language has a length that is exponential in the length of the original regexp. So while the regexp is guaranteed to exist, it is not guaranteed to be simple. It can be calculated algorithmically if you really need it.

The ^ operator to anchor a regexp isn't relevant. You may be thinking of ^ in a character set, as in [^a-z] meaning “any one character that isn't a lowercase letter”. This is just part of the shortcut notation for character sets, it isn't helpful to complement a set of strings.

Some regular expression engines, such as the widely-used PCRE, support additional operators beyond the traditional ones including lookaround assertions. A negative lookahead assertion provides a simple way to negate a regular expression without having to break it down into parts. Check the documentation of your software to see what kind of regular expressions it supports.

^!(.*timetosa).*$

Most systems don't need regexp complementation because you can achieve the same effect by using a matching inversion flag (e.g. grep -v) or by ordering rules carefully in a first-match setting (if it matches .*timetosa.* then do nothing and stop matching rules; if it matches .* then do something).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.