@@ -196,33 +196,24 @@ public static string ConvertFromDscObject(IEnumerable<Hashtable> dscResources, i
196196
197197 private static string RemoveModuleVersionInfo ( string content , List < string > ? uniqueModules = null )
198198 {
199- int start = 0 ;
200- do
199+ if ( uniqueModules is null || uniqueModules . Count == 0 )
201200 {
202- start = content . IndexOf ( "import-dscresource" , start , StringComparison . CurrentCultureIgnoreCase ) ;
203- if ( start >= 0 )
201+ return content ;
202+ }
203+
204+ string pattern = @"(import-dscresource\b[^\n]*?)\s+-moduleversion\s+(?:""[^""]*""|'[^']*'|\S+)([^\n]*)" ;
205+ return Regex . Replace ( content , pattern , match =>
206+ {
207+ string fullLine = match . Value ;
208+ foreach ( string module in uniqueModules )
204209 {
205- int end = content . IndexOf ( "\n " , start ) ;
206- if ( end > start )
210+ if ( fullLine . IndexOf ( module , StringComparison . CurrentCultureIgnoreCase ) >= 0 )
207211 {
208- foreach ( string module in uniqueModules ?? [ ] )
209- {
210- int moduleIndex = content . IndexOf ( module , start , StringComparison . CurrentCultureIgnoreCase ) ;
211- if ( moduleIndex >= 0 && moduleIndex < end )
212- {
213- start = content . IndexOf ( "-moduleversion" , start , StringComparison . CurrentCultureIgnoreCase ) ;
214- if ( start >= 0 && start < end )
215- {
216- content = content . Remove ( start , end - start ) ;
217- break ;
218- }
219- }
220- }
221- start += 1 ;
212+ return match . Groups [ 1 ] . Value + match . Groups [ 2 ] . Value ;
222213 }
223214 }
224- } while ( start >= 0 ) ;
225- return content ;
215+ return fullLine ;
216+ } , RegexOptions . IgnoreCase ) ;
226217 }
227218
228219 private static void InitializeCimClasses ( )
0 commit comments