regex个
import re
def remove_indefinite_part(input_string):
# Define the regular expression pattern to match the first indefinite part
pattern = r'^\/[a-zA-Z]+(@[a-zA-Z]+)?\s?'
# Find the first match using the pattern
match = re.search(pattern, input_string)
if match:
# Remove the matched part from the input string
modified_string = input_string[len(match.group()):].strip()
else:
modified_string = input_string
return modified_string
# Test the function with example inputs
inputs = [
'/a (some optional text)',
'/a@w (some optional text)',
'/an (some optional text)',
'/an@w (some optional text)',
'/am (some optional text)',
'/am@w (some optional text)',
'/b (some optional text)',
'/b@w (some optional text)',
'/bn (some optional text)',
'/bn@w (some optional text)',
'/bm (some optional text)',
'/bm@w (some optional text)'
]
for input_str in inputs:
result = remove_indefinite_part(input_str)
print(result)
输出:
(some optional text)
(some optional text)
(some optional text)
(some optional text)
(some optional text)
(some optional text)
(some optional text)
(some optional text)
(some optional text)
(some optional text)
(some optional text)
(some optional text)