-5

There are a lot of places that show you to match a string that does not contain a word. What I want to do is to use a regular expression to match a string and then test if that string contains a word.

In other words this is what am I looking for:

I have the text:

.........(note every chunk starts with <"number"><"hex number">)

<1><35c>: Abbrev Number: 7 (DW_TAG_array_type)
    <35d>   DW_AT_sibling     : <0x369> 
    <361>   DW_AT_type        : DW_FORM_ref4 <0x4d01>   
<2><366>: Abbrev Number: 8 (DW_TAG_subrange_type)
    <367>   DW_AT_upper_bound : 127 
<1><369>: Abbrev Number: 7 (DW_TAG_array_type)
    <36a>   DW_AT_sibling     : <0x377> 
    <36e>   DW_AT_type        : DW_FORM_ref4 <0x4d01>   
<2><373>: Abbrev Number: 8 (DW_TAG_subrange_type)
    <374>   DW_AT_upper_bound : 511 
<1><377>: Abbrev Number: 9 (DW_TAG_structure_type)
    <378>   DW_AT_sibling     : <0x4cb> 
    <37c>   DW_AT_name        : mem_pool    
    <385>   DW_AT_byte_size   : 68  
<2><386>: Abbrev Number: 10 (DW_TAG_member)
    <387>   DW_AT_type        : DW_FORM_ref4 <0x4d28>   
    <38c>   DW_AT_accessibility: 1  (public)
    <38d>   DW_AT_name        : Type    
    <392>   DW_AT_data_member_location: 2 byte block: 23 0  (DW_OP_plus_uconst: 0)
<1><357>: Abbrev Number: 9 (DW_TAG_structure_type)
    <37c>   DW_AT_name        : mem_pool2   
    <385>   DW_AT_byte_size   : 28  
<1><35c>: Abbrev Number: 7 (DW_TAG_array_type)
    <378>   DW_AT_sibling     : <0x4cb> 
    <37c>   DW_AT_name        : mem_pool    
    <385>   DW_AT_byte_size   : 68

then to get the chunks that have DW_TAG_structure_type I use the regex expression:

(?s)\n[^\n]+?DW_TAG_structure_type.*?(?=..\d+><)    

that matches:

1)

 <1><377>: Abbrev Number: 9 (DW_TAG_structure_type)
    <378>   DW_AT_sibling     : <0x4cb> 
    <37c>   DW_AT_name        : mem_pool    
    <385>   DW_AT_byte_size   : 68  

and

2)

<1><357>: Abbrev Number: 9 (DW_TAG_structure_type)
    <37c>   DW_AT_name        : mem_pool2   
    <385>   DW_AT_byte_size   : 28  

now my question is I will like to exclude the first match if it contains the string sibling. So what I have done to try to solve the problem was:

 (?s)(?!.*sibling)\n[^\n]+?DW_TAG_structure_type.*?(?=..\d+><)

note I added (?!.*sibling) to first look around to test that the word sibling is not there. that does not match anything though.

Edit

It will be nice if my first regular expression:

 (?s)\n[^\n]+?DW_TAG_structure_type.*?(?=..\d+><) 

I could capture it in a group and then test for what I need. Doing something like

 (?s)(\n[^\n]+?DW_TAG_structure_type.*?(?=..\d+><))(?=\1 "if group1 cointains sibling then..."
Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
Tono Nam
  • 29,637
  • 73
  • 252
  • 421
  • 6
    Why are you cramming all of this into a sigle Regex pattern? You have C# at your disposal, it's much more maintainable to call Regex a few different times with some C# logic in-between than trying to cram everything into a single pattern. Just match your original pattern and then do `if(result.ToLower().Contains("sibling")) { DoSomething(); }` – qJake Jun 28 '12 at 20:24
  • "*I could capture it in a group and then test for what I need.*" **You can do this with C#. Why are you not leveraging the power of the language you're coding with?** – qJake Jun 28 '12 at 20:26
  • I was hoping to learn... Before I was using look arounds with c#. when I learned how to use `(?='regex')` it saved me a lot of time. I will like to get better at regex – Tono Nam Jun 28 '12 at 20:31
  • Regex is not the end-all, be-all tool for matching every string out there. It is the correct tool for the job in *some cases*, but in others it is far more efficient and far easier to use the power of your programming language instead of trying to hack something together with Regex. – qJake Jun 28 '12 at 20:32
  • I agree with SpikeX's advice for when you're putting this code into production, but the question remains valid. Nothing wrong with learning more about regex. – C.Evenhuis Jun 28 '12 at 20:32
  • @C.Evenhuis True. Any time someone over-uses Regex I have to make the "wrong tool for the job" point, but if it's purely educational, then by all means, go for it. :) – qJake Jun 28 '12 at 20:46
  • OK but if it is for educational then it should get tagged homework – paparazzo Jun 28 '12 at 21:46
  • `Regex is not the end-all, be-all tool for matching every string out there.` Ye shall be struck down for thy blasphemy, @qJake! – Dan Wilson Dec 28 '16 at 20:00

1 Answers1

-1

Try this one: (?s)\n[^\n]+?DW_TAG_structure_type.*?\n(?![^\n]*sibling).*?(?=..\d+><)

Ωmega
  • 37,727
  • 29
  • 115
  • 183
  • Will get you back to even. That did not deserve a down vote. – paparazzo Jun 29 '12 at 23:48
  • Ωmega I did not down voted on your question. I should had tagged this question with the homework tag earlier I think as my purpose was to learn. I appreciate your help. I cannot make your answer work though... – Tono Nam Jul 02 '12 at 18:58