cdalsanto
cdalsanto

Reputation: 13

Regex Structural Search Not Containing Word

I'm trying to search through my Android project's XML files for objects that do not contain a certain object.

My set of objects looks like this:

<View
 android:id="@+id/obstacle3"
 android:layout_width="@dimen/obstacle_width"
 android:layout_height="@dimen/obstacle_width"
 android:background="@drawable/play_portal"
 android:layout_below="@+id/obstacle36"
 android:layout_toRightOf="@+id/obstacle29"
 ads:brother="@+id/obstacle21"
/>

<View
 android:id="@+id/obstacle3"
 android:layout_width="@dimen/obstacle_width"
 android:layout_height="@dimen/obstacle_width"
 android:background="@drawable/play_portal"
 android:layout_below="@+id/obstacle36"
 android:layout_toRightOf="@+id/obstacle29"
/>

<View
 android:id="@+id/obstacle3"
 android:layout_width="@dimen/obstacle_width"
 android:layout_height="@dimen/obstacle_width"
 android:background="@drawable/play_portal"
 android:layout_below="@+id/obstacle36"
 android:layout_toRightOf="@+id/obstacle29"
 ads:brother="@+id/obstacle21"
/>

With this search, <View(\s+[^>]*?)ads([^>]*?/>) I can find all Views that contain ads:brother attribute, but I want the inverse; I want to find all Views that do not contain ads:brother attribute.

I've tried numerous things to no avail. What am I doing wrong here?

Upvotes: 0

Views: 27

Answers (1)

ndnenkov
ndnenkov

Reputation: 36101

__Insert mandatory disclaimer "don't parse xml with regex" here__

<View([^>](?!ads))*?\/>

See it in action

The idea is to check that after each matched character there is no ads following, thus there is no ads in the entire match.

Upvotes: 1

Related Questions