We’re rewarding the question askers & reputations are being recalculated! Read more.

5 deleted 38 characters in body

Here is the complete program in MWE (minimum working example) form, with test cases:

Here is the complete program in MWE (minimum working example) form, with test cases:

Here is the complete program, with test cases:

4 edited title

# Simplify complicated Perl regular expression? (Converting Unicode fractions to TeX)

3 added 28 characters in body

I'm converting Unicode text to TeX for typesetting. In the input, I'm allowing simple fractions like ½ and ⅔ using single Unicode characters and complex fractions like ¹²³/₄₅₆ using superscripted and subscripted numerals. First I convert the simple fractions (½ becomes \frac{1}{2}) and superscripted and subscripted numerals (¹²³ becomes $^1$$^2^3$$^{123}$ and ₄₅₆ becomes $_4$$_5$$_6$$_{456}) using a character lookup table, then I make a second pass to collapse runs of numerals and combine numerator and denominator into a fraction (so ¹²³/₄₅₆ then becomes \frac{123}{456}). Finally, I make a third pass to insert a ⅙-em thin space between an integer and a fraction (so that, for example, 2¼ displays as 2 ¼). • In the first transformation, is there a way to avoid the alternation operator (|) and simply match on either \^ or \-\_? I don't see a way to do it using ([\^\_]) and \1. • Also in the first transformation, is there a way to avoid the nested substitutions? • Is there some completely other solution to this that would work even better? (By better I don't mean faster but easier to understand.) I'm converting Unicode text to TeX for typesetting. In the input, I'm allowing simple fractions like ½ and ⅔ using single Unicode characters and complex fractions like ¹²³/₄₅₆ using superscripted and subscripted numerals. First I convert the simple fractions (½ becomes \frac{1}{2}) and superscripted and subscripted numerals (¹²³ becomes ^1$$^2$^3$ and ₄₅₆ becomes $_4$$_5$$_6$) using a character lookup table, then I make a second pass to collapse runs of numerals and combine numerator and denominator into a fraction (so ¹²³/₄₅₆ then becomes \frac{123}{456}). Finally, I make a third pass to insert a ⅙-em thin space between an integer and a fraction (so that, for example, 2¼ displays as 2 ¼). • In the first transformation, is there a way to avoid the alternation operator (|) and simply match on either \^ or \-? I don't see a way to do it using ([\^\_]) and \1. • Also in the first transformation, is there a way to avoid the nested substitutions? • Is there some completely other solution to this that would work even better? (By better I don't mean faster but easier to understand.) I'm converting Unicode text to TeX for typesetting. In the input, I'm allowing simple fractions like ½ and ⅔ using single Unicode characters and complex fractions like ¹²³/₄₅₆ using superscripted and subscripted numerals. First I convert the simple fractions (½ becomes \frac{1}{2}) and superscripted and subscripted numerals (¹²³ becomes $^{123}$ and ₄₅₆ becomes $_{456}\$) using a character lookup table, then I make a second pass to collapse runs of numerals and combine numerator and denominator into a fraction (so ¹²³/₄₅₆ then becomes \frac{123}{456}). Finally, I make a third pass to insert a ⅙-em thin space between an integer and a fraction (so that, for example, 2¼ displays as 2 ¼).

• In the first transformation, is there a way to avoid the alternation operator (|) and simply match on either \^ or \_? I don't see a way to do it using ([\^\_]) and \1.
• Also in the first transformation, is there a way to avoid the nested substitutions?
• Is there some completely other solution to this that would work even better? (By better I don't mean faster but easier to understand.)
2 added 28 characters in body
1