Home > Mobile >  How to filter out results contains certain string in vim regex?
How to filter out results contains certain string in vim regex?

Time:11-05

Suppose I have a bunch of code, the format is like verilog input/output (wire {signal_name}(,).

input  wire aaa,
input  bbb,               // "wire" can be omitted 
input  wire ccc           // the comma can be omitted
input  wire ddd_0,
output ddd_1  
output wire ddd_1,
output wire EEE_E,

And I want to capture the signal name such as aaa, bbb ... EEE_E My regex is:

/\v(((input)|(output))[ ] (wire)*[ ]*)@<=([_0-9a-zA-Z] )

But it seems that "wire" itself also matchs this pattern and will be captured.
I can use the '\n' '",\n"' to make sure no "wire" will be captured because there is never '\n' or '",\n"' behind "wire". But is there any general method to filter out certain strings or certain patterns in vim regex?

outcomes.filter_out("wire") 

Maybe I need a simpler example? Suppose I have a bunch of "word" composed by {'a'-'z', '0'-'9', '_', '$', '%'}

11 apple pear banan0 wire _work m$oney && books wire word air 22

If I use
/\v(\s|^)@<=([a-z]*)\ze(\s|$),

I got all "pure" words:
apple pear wire books wire word air.

But, what can I do if I don't want wire matched, only:
apple pear books word air

CodePudding user response:

\(in\|out\)put\s\ \(wire\s*\)*\zs\([^, ]\ \)

seems to do what you want (or at least what I think you want):

capture

As for your question:

But is there any general method to filter out certain strings or certain patterns in vim regex?

Your example doesn't seem to involve regex at all so I am not sure what it is you are asking for.

CodePudding user response:

To find the words apple pear books word air or wires in the sentence:

11 apple pear banan0 wire _work m$oney && books wire word air 22

You can use lookarounds and \v for very magic mode

\v\S@<!(wire\S@!)@![a-z] \S@!

Explanation

  • \v Very magic mode
  • \S@<! Negative lookbehind, assert a whitespace boundary to the left
  • (wire\S@!) Negative lookahead, assert not the word wire directly to the right followed by a whitespace boundary
  • [a-z$%] Match 1 or more of the listed character in the character class
  • \S@! Negative lookahead, assert a whitespace boundary to the right

enter image description here

You can extend this mechanism to match the input/output examples in the first part of your question.

Note that to see the highlights as in the attached image, you can run :set hls

  • Related