Search code examples
regexbackreference

Regexoptional group backreference


as a translation company, we spend a lot of time preparing Indesign files for translation. For this, we've already created several JAVA scripts. One of the things I'd like to achieve is replacing decimal dots with spaces. This works fine for normal numbers (8.480 or 8.487) with simple regex like ^(\d+)(\.)(\d+)$

However, we also have number combinations like these: 1.240x899x987 or 987x1.048x987. Some or all of the numbers might or might not contain a decimal dot. I can search for the numbers by using optional groups fine using: ^(\d+)(\.?)(\d+)(x)(\d+)(\.?)(\d+)(x)(\d+)(\.?)(\d+)$

I am running into problems replacing these string using numbered backreferences. If the optional group is not null, it takes a numbered reference, and $1\s$3$4 returns 1 240x. However, if the group is null, the backreference numbers all get changed.

Is there any way to check for null (if not null then) in the regex? So I could say 'if the optional group is not null, replace it with space'? In short, I need a way to change 1.287x989x150 and 987x1.048x987 and 987x313x1.487 to spaces instead of dot decimal in a single regex? Is this even possible?

For now, I've split the three cases into separate search-and-replace GREP's in a javascript in a non-greedy fashion (matching the exact pattern for these three occurrences), but If possible, and if-clause would be much neater.

Kind regards


Solution

  • Why not simplify things and replace all dots with spaces iff the dots are surrounded by digits? You can do this by searching for

    (?<=\d)\.(?=\d)
    

    and replacing all with a simple " ".