In the current version of my book I use static referencing but now I want to use dynamic referencing using \label and \ref and performing the appropriate substitutions. Some substitution examples are the following:

Figure 5-6 -----> Figure \ref{0506}
Figure 2-17 ----> Figure \ref{0217}
Figure 14-4 ----> Figure \ref{1404}
Figure 34-45 --> Figure \ref{3445}
Figure 12-109 ---> Figure \ref{12109}

Since there are about 2000 such cases I want a regular expression for this (preferably in WinEdt). I was not able to construct such an expression. Can anyone help me with this? Thanks a lot!

  • 1
    What have you tried? What's the issue here? From your sample it would seem to be best to add a leading zero to entries with single digits on either side of the dash (using regular expressions, of course). Once you have that (i.e. Figure 05-06 and Figure 02-17 etc.) you simply search for a regex like Figure ([0-9]+)-([0-9]*) and replace with Figure \\ref{\1\2} or similar – unless WinEdt uses a different syntax, I am not familiar with that program.
    – Ingmar
    Commented Feb 27, 2022 at 20:22
  • I tried to create a regular expression in which the first digit is optional in order to cover both cases but it did not worked correctly. I will try the leading zero approach, hoping to work. Thanks a lot !! Commented Feb 27, 2022 at 20:57
  • It's a bit more difficult, since you'd have to have an optional zero in your replacement part. It took me three search & replaces to account for all your examples.
    – Ingmar
    Commented Feb 27, 2022 at 21:10
  • 1
    while it may make this regex replace easier note that it is normally bad style to use the figure numbers as the \label reference. \ref{3445} will still work if the document is edited and that figure becomes Figure 35-22, but it will be very confusing for any human reading the source. Commented Feb 27, 2022 at 21:34

2 Answers 2


I would do this in three stages, I'll use sed syntax but regex are more or less portable across systems.

  1. pad the single digit cases with zero (first part)

     $ echo 'Figure 5-6' | sed -e 's/Figure \([0-9]\)-/Figure 0\1-/'
     Figure 05-6
  2. pad the single digit cases with zero (second part)

     $ echo 'Figure 05-6 ' | sed -e 's/Figure \([0-9]\+\)-\([0-9][^0-9]\)/Figure \1-0\2/'
     Figure 05-06 
  3. Then convert to \ref

    $ echo 'Figure 05-06 ' | sed -e 's/Figure \([0-9]\+\)-\([0-9]\+\)/Figure \\ref{\1\2}/'
     Figure \ref{0506} 

@David Carlisle I did the same thing in Winedt (back references \1, \2, etc)

Case 1 ---> Figure \(1[0-9]\)-\(2[0-9]\)~[0-9]   e.g. 2-4.  ===> Figure 0\1-0\2     and then  Figure \\ref\{0\10\2\}       
Case 2 ---> Figure \(1[0-9]\)-\(2[0-9][0-9]\)~[0-9]   e.g. 2-14.  ===> Figure 0\1-\2      and then  Figure \\ref\{0\1\2\}         
Case 3 ---> Figure \(1[0-9][0-9]\)-\(2[0-9]\)~[0-9]   e.g. 12-4.  ===> Figure \1-0\2      and then  Figure \\ref\{\10\2\}         
Case 4 ---> Figure \(1[0-9][0-9]\)-\(2[0-9][0-9]\)~[0-9]   e.g. 12-14.   ===> Figure \1-\2       and then  Figure \\ref\{\1\2\}

(of course in the fourth case the middle step is not necessary).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .