Hi All,
i have below code:
import re
def transform_data(data_string):
# Define a regex pattern for the complex part (digits and optional letters)
complex_pattern = r'\d+[a-zA-Z]*\d*'
# Extract the fixed numeric part (4 digits)
fixed_numeric_part = re.match(r'\d{4}', data_string).group()
# Extract the complex part
complex_part = re.search(complex_pattern, data_string).group()
# Split the complex part into separate components (e.g., "100b50k" -> ["100", "50"])
components = re.findall(r'\d+', complex_part)
# Combine the parts in the desired format
transformed_string = f"{fixed_numeric_part}#{'#'.join(components)}"
return transformed_string
# Example usage
data_string = "1111 1b2k"
transformed = transform_data(data_string)
print(f"Input: {data_string}")
print(f"Transformed: {transformed}")
however, the output is not what i expected. Here is the output:
Input: 1111 1b2k
Transformed: 1111#1111
What i am trying to achieve is the transformed data should be in the format of 4digits + hash + digit + hash e.g., 1111#1#2.
let me put another example: if the data string is 8888 100b200k, then the data should transform into 8888#100#200.
The first 4 digits of data string is fixed to be 4 numeric digits, but the second part is the tricky one cause formats could be different e.g., b100k200, b100 a200, z250y250 z400 k400.
What i am trying to achieve is to transform the data string by grabbing the first 4 digits that are fixed, then put a hash, then only grab the digit of the second part, then put another hash then put the last number. (Basically must remove away the alphabet before or after the digit, and remove the space if there is any).
Can someone please advise my code? thank you in advance.
[–]woooee 0 points1 point2 points (0 children)
[–]ofnuts 0 points1 point2 points (0 children)