Can I use .str.contains to test against multiple different regexes at once and flag if…
- User
Can I use .str.contains to test against multiple different regexes at once and flag if at least one is present?
Assistant
Yes, you can test multiple regexes at once with
.str.contains()by combining them with the pipe operator|(which means "OR" in regex):import pandas as pd df = pd.DataFrame({'text': ['apple pie', 'banana bread', 'cherry tart', 'date cake']}) # Test if text contains either 'apple' or 'banana' or 'cherry' df['has_fruit'] = df['text'].str.contains('apple|banana|cherry') # With more complex patterns df['has_pattern'] = df['text'].str.contains('a.*e|b.*d|c.*y')For more complex scenarios, you can also:
- Use a list of patterns:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.