Regula expressions: How to find a string without some other string

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Regula expressions: How to find a string without some other string

Igor Forca
Hi,
I have a log file with several text OK and error messages.

OK message is always: AAA000A

Error message starts with AAA, then three numbers (but not three zeros) and letter A.
Error sample: AAA001A

From the Vim Regular Expression web site:
http://vimregex.com/#metacharacters
I can see:
\d digit
\D non-digit

To find OK and error message in vim:
/AAA\d\d\d\D

but how to get only error messages.

Something like:
- use: /AAA\d\d\d\D
- except: /AAA000A

Regards

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regula expressions: How to find a string without some other string

Christian Brabandt

On Mi, 05 Jul 2017, Igor Forca wrote:

> Hi,
> I have a log file with several text OK and error messages.
>
> OK message is always: AAA000A
>
> Error message starts with AAA, then three numbers (but not three zeros) and letter A.
> Error sample: AAA001A
>
> From the Vim Regular Expression web site:
> http://vimregex.com/#metacharacters
> I can see:
> \d digit
> \D non-digit
>
> To find OK and error message in vim:
> /AAA\d\d\d\D

This will find okay or error messages.

> but how to get only error messages.
>
> Something like:
> - use: /AAA\d\d\d\D
> - except: /AAA000A

Something like this should work:
A\{3}\%(\([^0]\d\{2}\)\|\(\d[^0]\d\)\|\(\d\{2}[^0]\)\)A
which matches 3 As, followed by either:
- a non-zero followed by 2 digits followed by another A or
- a digit followed by a non-zero followed by another digit and A or
- 2 digits followed by a non-zero followed by another A


Best,
Christian
--
Warum überquerte das Huhn die Straße?
Kindergärtnerin:
  Um auf die andere Straßenseite zu kommen.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regula expressions: How to find a string without some other string

Erik Christiansen
In reply to this post by Igor Forca
On 05.07.17 00:25, Igor Forca wrote:
> To find OK and error message in vim:
> /AAA\d\d\d\D
>
> but how to get only error messages.

As you're seeking a numeric (not regex) exclusion, I'd skip complex
regex gymnastics, and just pipe the log file through "grep -v AAA000A"
before using your simple regex, now with no OK messages to confuse
matters.

If that doesn't appeal, just globally s/AAA000A/AAA_OK, before running
your regex. That way an undo restores vim's text buffer, if any reason
for doing that comes to mind. In neither case is the log file affected.

Erik

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regula expressions: How to find a string without some other string

Igor Forca
@Erik, I know all of this techniques, but I would like to LEARN the vim way. The vim way may be very easy if commands stored in .vim file and executed with key mappings.

@Christian, interesting technique and working fine. Just wondering what is the purpose of \%, it is working with or without this string.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regula expressions: How to find a string without some other string

Shawn H Corey
On Wed, 5 Jul 2017 03:33:16 -0700 (PDT)
Igor Forca <[hidden email]> wrote:

> @Christian, interesting technique and working fine. Just wondering
> what is the purpose of \%, it is working with or without this string.

:help \%(


--
Don't stop where the ink does.

        Shawn H Corey

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regula expressions: How to find a string without some other string

Nikolay Aleksandrovich Pavlov
In reply to this post by Christian Brabandt
2017-07-05 10:49 GMT+03:00 Christian Brabandt <[hidden email]>:

>
> On Mi, 05 Jul 2017, Igor Forca wrote:
>
>> Hi,
>> I have a log file with several text OK and error messages.
>>
>> OK message is always: AAA000A
>>
>> Error message starts with AAA, then three numbers (but not three zeros) and letter A.
>> Error sample: AAA001A
>>
>> From the Vim Regular Expression web site:
>> http://vimregex.com/#metacharacters
>> I can see:
>> \d digit
>> \D non-digit
>>
>> To find OK and error message in vim:
>> /AAA\d\d\d\D
>
> This will find okay or error messages.
>
>> but how to get only error messages.
>>
>> Something like:
>> - use: /AAA\d\d\d\D
>> - except: /AAA000A
>
> Something like this should work:
> A\{3}\%(\([^0]\d\{2}\)\|\(\d[^0]\d\)\|\(\d\{2}[^0]\)\)A
> which matches 3 As, followed by either:
> - a non-zero followed by 2 digits followed by another A or
> - a digit followed by a non-zero followed by another digit and A or
> - 2 digits followed by a non-zero followed by another A

These are too much combinations. I would suggest using lookahead instead:

    \vAAA%(.{,2}[1-9])@=\d{3}A

I.e. three A’s followed by three digits and A where position just
after three A’s is occupied by zero..two characters followed by 1-9.

You already have three branches for three-digit error code. Will have
four branches for four-digit, etc. With lookahead you always need only
one lookahead for any given number of digits.

There are also concats as an alternative to positive lookahead, but
since they are not in PCRE and do not present anything new I do not
use them.

>
>
> Best,
> Christian
> --
> Warum überquerte das Huhn die Straße?
> Kindergärtnerin:
>   Um auf die andere Straßenseite zu kommen.
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regula expressions: How to find a string without some other string

Igor Forca
> These are too much combinations. I would suggest using lookahead instead:
>     \vAAA%(.{,2}[1-9])@=\d{3}A
Above procudes an error "E486 Pattern not found".

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regula expressions: How to find a string without some other string

Igor Forca
I got excellent answer using NEGATIVE, from Jürgen on my private e-mail.

>I would use negative look-ahead (":help /\@!"):
>  /AAA\(000\)\@!\d\d\d\D/
>
>This matches 3 "A"s, three digits, which must not be "000", and one non-digit.
>Note that \(000\)\@! does not consume  any character, thus "\d\d\d" will match
>at the same position where "\(000\)\@!" matched.
>
>Regards,
>Jürgen

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regula expressions: How to find a string without some other string

'Jürgen Krämer' via vim_use

Hi,

Igor Forca schrieb am 05.07.2017 um 14:40:

> I got excellent answer using NEGATIVE, from Jürgen on my private e-mail.
>
>> I would use negative look-ahead (":help /\@!"):
>>  /AAA\(000\)\@!\d\d\d\D/
>>
>> This matches 3 "A"s, three digits, which must not be "000", and one non-digit.
>> Note that \(000\)\@! does not consume  any character, thus "\d\d\d" will match
>> at the same position where "\(000\)\@!" matched.
>>
>> Regards,
>> Jürgen
>

strange, I didn't intend to send that mail privately. Must have hit
the wrong button.

Regards,
Jürgen

--
~
~
~
:wq

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regula expressions: How to find a string without some other string

Ben Fritz
In reply to this post by Igor Forca
On Wednesday, July 5, 2017 at 7:38:58 AM UTC-5, Igor Forca wrote:
> > These are too much combinations. I would suggest using lookahead instead:
> >     \vAAA%(.{,2}[1-9])@=\d{3}A
> Above procudes an error "E486 Pattern not found".

Works fine for me. Did you leave out the '\v' by accident? Those two characters let you omit the '\' in many places making the regex a little more readable.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...