@@ -381,8 +381,35 @@ def generate_id_from_regex(self, regex_pattern): # noqa: C901
381381 while i < len (pattern ):
382382 char = pattern [i ]
383383
384+ # Handle groups with alternation (...)
385+ if char == "(" :
386+ # Find the matching closing parenthesis
387+ depth = 1
388+ end = i + 1
389+ while end < len (pattern ) and depth > 0 :
390+ if pattern [end ] == "(" :
391+ depth += 1
392+ elif pattern [end ] == ")" :
393+ depth -= 1
394+ end += 1
395+
396+ group_content = pattern [i + 1 : end - 1 ]
397+ i = end
398+
399+ # Check for alternation (|)
400+ if "|" in group_content :
401+ # Split by | and choose one randomly
402+ alternatives = group_content .split ("|" )
403+ chosen = random .choice (alternatives )
404+ result .append (chosen )
405+ else :
406+ # No alternation, just recursively generate from the group content
407+ generated = self .generate_id_from_regex ("^" + group_content + "$" )
408+ if generated :
409+ result .append (generated )
410+
384411 # Handle character classes [...]
385- if char == "[" :
412+ elif char == "[" :
386413 end = pattern .index ("]" , i )
387414 char_class = pattern [i + 1 : end ]
388415 i = end + 1
@@ -408,11 +435,23 @@ def generate_id_from_regex(self, regex_pattern): # noqa: C901
408435 # Handle negation [^...]
409436 if char_class .startswith ("^" ):
410437 result .append (random .choice ("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ))
411- # Handle ranges like [A-Z], [0-9], [a-z]
412- elif "-" in char_class and len (char_class ) == 3 and char_class [1 ] == "-" :
413- start_char = ord (char_class [0 ])
414- end_char = ord (char_class [2 ])
415- result .append (chr (random .randint (start_char , end_char )))
438+ # Handle character classes with ranges (parse them properly)
439+ elif "-" in char_class :
440+ # Expand all ranges in the character class
441+ chars = []
442+ j = 0
443+ while j < len (char_class ):
444+ # Check if this is a range (e.g., A-Z or 0-9)
445+ if j + 2 < len (char_class ) and char_class [j + 1 ] == "-" :
446+ start_char = ord (char_class [j ])
447+ end_char = ord (char_class [j + 2 ])
448+ chars .extend ([chr (c ) for c in range (start_char , end_char + 1 )])
449+ j += 3
450+ else :
451+ # Single character (not part of a range)
452+ chars .append (char_class [j ])
453+ j += 1
454+ result .append (random .choice (chars ))
416455 # Handle explicit character list [ABC123]
417456 else :
418457 result .append (random .choice (char_class ))
0 commit comments