Two questions about integral "splitting ring" extensions
The following is a string expression which I think does what you describe:
StringReplace[data,
"N" ~~ d : DigitCharacter ~~ x : Except[DigitCharacter] :>
"N0" <> d <> x]
Note that StringReplace
does automatically map onto lists of strings (which is documented), so you don't need to Map
it onto the list of strings data
.
If you prefer regular expressions here is the same expressed with regular expressions:
StringReplace[data, RegularExpression["N(\\d)([^\\d])"] :> "N0$1$2"]
These do both work for your example data, but won't replace when the pattern is at the end of a string, like in "N1 N2 G01 X35 Y51 N3"
, if you want to also handle those, that can be either achieved with an additional rule:
StringReplace[data, {
"N" ~~ d : DigitCharacter ~~ x : Except[DigitCharacter] :>
"N0" <> d <> x,
"N" ~~ d : DigitCharacter ~~ EndOfString :> "N0" <> d
}]
StringReplace[data, {
RegularExpression["N(\\d)([^\\d])"] :> "N0$1$2",
RegularExpression["N(\\d)$"] :> "N0$1"
}]
or with a slightly more complicated pattern:
StringReplace[data,
"N" ~~ d:DigitCharacter ~~ x:(Except[DigitCharacter] | EndOfString) :>
"N0" <> d <> x]
StringReplace[data,RegularExpression["N(\\d)([^\\d]|$)"] :> "N0$1$2"]