Split String on Comma and Dot – Detailed Regex Split in Python #Python #Regex #Split

preview_player
Показать описание
In this problem, you are given a string that consists only of digits (0-9), commas (,) and dots (.). Every comma and dot is guaranteed to be preceded and followed by a digit. Your task is to split the string using these comma and dot symbols as delimiters.

Step-by-Step Explanation:

Input Handling:

The input is provided as a single line containing space-separated numbers with commas and dots.

For example, the input might be:
100,000,000.000
This represents a continuous string where commas and dots separate groups of digits.

The regex pattern used is:
r'[,.]'

This pattern matches any occurrence of a comma or a dot. Since the problem guarantees that each comma and dot is flanked by digits, this simple pattern suffices to split the string into the desired substrings.

Output Formatting:

The solution then prints each substring on a new line, preserving the order of appearance.

This detailed approach illustrates the fundamentals of using regular expressions for string splitting, making it ideal for understanding text processing in Python.

Code (Detailed Version):

import re

def solve():
# Read the input string
s = input().strip()
# Define regex pattern to split on comma or dot
pattern = r'[,.]'
# Print each substring on a new line
for item in result:
print(item)

if __name__ == '__main__':
solve()
Рекомендации по теме
visit shbcf.ru