[파이썬] 반복 횟수 지정(*, +, ?, {m,n})

When working with strings or patterns in Python, it is often useful to repeat a certain element multiple times. Python provides several ways to specify the number of repetitions using special characters such as *, +, ?, {m,n}. In this blog post, we will explore these repetition operators and how they can be used in Python.

The Asterisk (*) Operator

The asterisk operator (*) is used to specify that the preceding element in a pattern can occur zero or more times. This means that the element can be repeated any number of times or it can be completely absent.

Here’s an example of using the asterisk operator in Python:

import re

# Match any number of 'a' characters followed by 'b' in the given string
pattern = re.compile("a*b")

# Search for the pattern in the string
result = pattern.search("bb ab aab abb")

# Print the matching result
print(result.group())  # Output: 'ab'

In the above example, the pattern a*b matches any number of a characters followed by a b. The a characters can occur zero or more times, allowing the pattern to match "ab", "aab", and "abb" in the given string.

The Plus (+) Operator

The plus operator (+) is similar to the asterisk operator, but it requires the preceding element to occur at least once. This means that the preceding element cannot be absent in the pattern.

Here’s an example of using the plus operator in Python:

import re

# Match one or more 'a' characters followed by 'b' in the given string
pattern = re.compile("a+b")

# Search for the pattern in the string
result = pattern.search("ab aab abb")

# Print the matching result
print(result.group())  # Output: 'ab'

In the above example, the pattern a+b matches one or more a characters followed by a b. The pattern will not match "ab" because it requires at least one occurrence of the a character.

The Question Mark (?) Operator

The question mark operator (?) is used to specify that the preceding element in a pattern can occur zero or one time. This means that the element is optional and can be present or absent.

Here’s an example of using the question mark operator in Python:

import re

# Match an optional 'a' character followed by 'b' in the given string
pattern = re.compile("a?b")

# Search for the pattern in the string
result = pattern.search("ab b")

# Print the matching result
print(result.group())  # Output: 'ab'

In the above example, the pattern a?b matches an optional a character followed by a b. The pattern can match "ab" or just "b", as the presence of the a character is optional.

The Curly Braces ({m,n}) Operator

The curly braces operator ({m,n}) is used to specify a specific range of repetitions for the preceding element in a pattern. The lower and upper range limits are specified as m and n, respectively.

Here’s an example of using the curly braces operator in Python:

import re

# Match 2 to 4 'a' characters followed by 'b' in the given string
pattern = re.compile("a{2,4}b")

# Search for the pattern in the string
result = pattern.search("abb aabb aaabb aaaabb aaaaabb")

# Print the matching result
print(result.group())  # Output: 'aaabb'

In the above example, the pattern a{2,4}b matches 2 to 4 a characters followed by a b. The pattern matches "aaabb" because it falls within the range of 2 to 4 a characters, whereas "abb", "aabb", and "aaaabb" do not meet the repetition criteria.

Conclusion

The repetition operators *, +, ?, {m,n} in Python provide flexibility when working with strings and patterns. By specifying the desired repetition of elements, you can effectively match patterns and extract relevant information from your data. Remember to use these operators wisely to ensure your patterns accurately capture the required repetitions.