I have some measurements of objects using W, L, H an D for width, length, etc. I want to move inch and feet symbols (", ') immediately next to the number and move those letters immediately next to the inch or feet symbols:
Example input:
09 ' 09 " 09 ' 09 "Test 09 " W a 09" W a
09 ' 09" 09 ' 09 "Test 09 " L a 09" w a
09 ' 09 " 09 ' 09 "Test 09 " L a 09" h a
09 ' 09 " 09 ' 09 "Test 09 " l a 09" d a
09 ' 09 " 09 ' 09 "Test 09 "word
Desired output:
09' 09" 09' 09" Test 09"W a 09"W a
09' 09" 09' 09" Test 09"L a 09"w a
09' 09" 09' 09" Test 09"L a 09"h a
09' 09" 09' 09" Test 09"l a 09"d a
09' 09" 09' 09" Test 09" word
I have tried multiple variations of this expression, every single one of which worked on regex101.com but not on actual Python:
Pattern: r'(?<=[0-9])( )?("|')((( )?([wWlLhHdD])( ))|( )?)(.)?'
Replacement: \2\6 \9
Actual input:
3/8 "W x 20" H -8 "Cortador de haste para madeira
Actual output:
3/8"W X 20"H -8 "Cortador de Haste p/ Madeira
Desired output:
3/8"W X 20"H -8" Cortador de Haste p/ Madeira
Notice how it almost works except that the very last " is not separated from the word and brought together with the number. What am I doing wrong? Since I am learning regex, I am as interested in my error lies as in an alternative, possibly more efficient solution.
Code:
new_title = re.sub(regex_prefix keyword_to_replace, keyword_to_replace_with, new_title, re.IGNORECASE)
CodePudding user response:
I am not sure which exact command you tried, but I would likely use the following:
# input
s='3/8 "W x 20" H -8 "Cortador de haste para madeira'
import re
re.sub(r'(?<=\d)\s*([\'"])\s*([wWlLhHdD]\b)?\s*', r'\1\2 ', s)
Output: '3/8"W X 20"H -8" Cortador de Haste p/ Madeira'