Regular Expression Quandry

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Greetings -- I need Regex help!!

For the following string,
"ANCHOR, STEER HORN, 2 1/2 IN X 3 IN X 3/15 IN DIA, 304 SSTL, UNEQUAL LEGS, F/ CC1 & 2 REGEN FLUE GAS LINES C 21, EQUIP W/ 4 IN REFRACTORY"

I need to remove all spaces EXCEPT in cases where the pattern \d\s\d exists (otherwise, from the example, "2 1/2" would become "21/2" which obviously has a very different meaning).

I can't figure out what the match expression is that will enable this functionality to work.

tia,

_howard
 
I think this should do it:
"\s+([^\s]|$)(?<!\d\s+\d)" -> "$1"
Read:
Any number of spaces (\s+),
followed by a non-space character ([^\s]) or the end of the string ($),
and: check if this position does not match \d\s+\d (negative lookbehind);

But it's probably more readable if you make a multi-pass algorithm:
- replace "\d\s\d" with e.g. "\dXXX\d" (use some unique string for "XXX")
- remove spaces
- replace "\dXXX\d" back to "\d \d"

Niki

Howard Dierking said:
Greetings -- I need Regex help!!

For the following string,
"ANCHOR, STEER HORN, 2 1/2 IN X 3 IN X 3/15 IN DIA, 304 SSTL, UNEQUAL
LEGS, F/ CC1 & 2 REGEN FLUE GAS LINES C 21, EQUIP W/ 4 IN REFRACTORY"
I need to remove all spaces EXCEPT in cases where the pattern \d\s\d
exists (otherwise, from the example, "2 1/2" would become "21/2" which
obviously has a very different meaning).
 
Back
Top