digitalmars.D.learn - std.regex.replace issues
- Andrej Mitrovic (29/30) Mar 17 2011 I've tried using this:
- Jesse Phillips (2/14) Mar 17 2011 Replace should probably work with char[], but not for efficiency. Unless...
- Andrej Mitrovic (3/3) Mar 17 2011 You're right, that point totally slipped my mind. I'm having some
- Jesse Phillips (2/6) Mar 17 2011 Why, if you want to operate on a dchar[] instead of a dchar range you ca...
- Andrej Mitrovic (1/1) Mar 17 2011 Okie.
I've tried using this: auto file = File("file.cpp", "r"); char[] replacement; foreach (char[] line; file.byLine()) { replacement = line.replace(regex("//.*"), "."); // do something with replacement while its still alive.. } This gives me this bombshell of an error: D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(2666): Error: cannot implicitly convert expression (result) of type string to char[] D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(2571): Error: template instance std.regex.RegexMatch!(char[]).RegexMatch.replace3!(string) error instantiating D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(1838): instantiated from here: replace!(string) D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(2854): instantiated from here: replaceAll!(string) qttod.d(15): instantiated from here: replace!(char[],Regex!(char),string) D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(1838): Error: template instance std.regex.RegexMatch!(char[]).RegexMatch.replace!(string) error instantiating D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(2854): instantiated from here: replaceAll!(string) qttod.d(15): instantiated from here: replace!(char[],Regex!(char),string)Exit code: 1I want to compose with `replace`. So I figured using char[]'s would avoid duplication. I want to use code like this: char[] replacement; foreach (char[] line; file.byLine()) { replacement = line.replace(regex("->"), ".") .replace(regex("::"), ".") .replace(regex("//.*"), "") .replace(regex("\\*"), ""); // do something with replacement while its still alive.. } How would I go about achieving this? And if I use strings instead isn't this going to involve a lot of duplication?
Mar 17 2011
Andrej Mitrovic Wrote:char[] replacement; foreach (char[] line; file.byLine()) { replacement = line.replace(regex("->"), ".") .replace(regex("::"), ".") .replace(regex("//.*"), "") .replace(regex("\\*"), ""); // do something with replacement while its still alive.. } How would I go about achieving this? And if I use strings instead isn't this going to involve a lot of duplication?Replace should probably work with char[], but not for efficiency. Unless I am wrong you will still cause a large amount of copying when replacing a single/multiple string with a multiple/single string. You may avoid re-allocation, but even that isn't guarantied.
Mar 17 2011
You're right, that point totally slipped my mind. I'm having some ideas about an alternative though. I'll try something out later. Btw, shouldn't byLine be able to return a dchar[]? For Unicode purposes?
Mar 17 2011
Andrej Mitrovic Wrote:You're right, that point totally slipped my mind. I'm having some ideas about an alternative though. I'll try something out later. Btw, shouldn't byLine be able to return a dchar[]? For Unicode purposes?Why, if you want to operate on a dchar[] instead of a dchar range you can do the conversion yourself with to!(dstring). byLine should return the data it is iterating over, not some conversion.
Mar 17 2011