Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 12, 2007 Regexp | ||||
---|---|---|---|---|
| ||||
I've got another question regarding regexp: If my document is full of variables such as "##var1 " and I want to replace them, with say "<var>var1</var>" how would I go about doing that? The variables now are preceded with two pound signs and ended with a single space. Also, could you please explain how the regexp function that you give me works? I'm trying to understand these, but think I'm not grasping the concept. Thanks! |
July 12, 2007 Re: Regexp | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | okibi wrote: > I've got another question regarding regexp: > > If my document is full of variables such as "##var1 " and I want to replace them, with say "<var>var1</var>" how would I go about doing that? The variables now are preceded with two pound signs and ended with a single space. > > Also, could you please explain how the regexp function that you give me works? I'm trying to understand these, but think I'm not grasping the concept. > > Thanks! I'm no regexp expert but I did something similar last night. You can use std.regexp.sub, eg. std.regexp.sub(document, r"##([^ ]+) ", r"<var>$1</var>", "gi"); document - your complete document, or part of it. "gi" - global and case insensitive. global means look for the string more than once case insensitive is self explanatory r"##([^ ]+) " - wysiwyg pattern: ## - look for # followed by # ( and ) - forms a group which can be pasted into result [^ ]+ - 1 or more characters which are NOT space - the pattern finishes with a space r"<var>$1</var>" - wysiwyg format: <var> - literal $1 - replace with first group from pattern </var> - literal So, it should look through document for the pattern "##([^ ]+) " and replace it with the format "<var>$1</var>" I tested this with: import std.regexp, std.stdio; string document = "This is a test ##bob and ##fred went for a walk to ##place how happy I am"; string pattern = r"##([^ ]+) "; string format = r"<var>$1</var>"; void main() { writefln(std.regexp.sub(document, pattern, format, "gi")); } Seems to work. :) Regan |
July 12, 2007 Re: Regexp | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Thanks, that's exactly what I was looking for!
Thanks also for the explanation of how the function works. That does make regexp a little easier now and I'll try to get the concept down.
Thanks!
Regan Heath Wrote:
> okibi wrote:
> > I've got another question regarding regexp:
> >
> > If my document is full of variables such as "##var1 " and I want to replace them, with say "<var>var1</var>" how would I go about doing that? The variables now are preceded with two pound signs and ended with a single space.
> >
> > Also, could you please explain how the regexp function that you give me works? I'm trying to understand these, but think I'm not grasping the concept.
> >
> > Thanks!
>
> I'm no regexp expert but I did something similar last night.
>
> You can use std.regexp.sub, eg.
>
> std.regexp.sub(document, r"##([^ ]+) ", r"<var>$1</var>", "gi");
>
> document - your complete document, or part of it.
>
> "gi" - global and case insensitive.
> global means look for the string more than once
> case insensitive is self explanatory
>
>
> r"##([^ ]+) " - wysiwyg pattern:
> ## - look for # followed by #
> ( and ) - forms a group which can be pasted into result
> [^ ]+ - 1 or more characters which are NOT space
> - the pattern finishes with a space
>
> r"<var>$1</var>" - wysiwyg format:
> <var> - literal
> $1 - replace with first group from pattern
> </var> - literal
>
> So, it should look through document for the pattern "##([^ ]+) " and replace it with the format "<var>$1</var>"
>
> I tested this with:
>
> import std.regexp, std.stdio;
>
> string document = "This is a test ##bob and ##fred went for a walk to
> ##place how happy I am";
> string pattern = r"##([^ ]+) ";
> string format = r"<var>$1</var>";
>
> void main()
> {
> writefln(std.regexp.sub(document, pattern, format, "gi"));
> }
>
> Seems to work. :)
>
> Regan
|
July 12, 2007 Re: Regexp | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | okibi wrote:
> Thanks, that's exactly what I was looking for!
>
> Thanks also for the explanation of how the function works. That does make regexp a little easier now and I'll try to get the concept down.
I find regexp hard to get my head round too. I think it's because I haven't done much perl and I have for the most part used windows. Therefore, I haven't had much regexp exposure.
However, having started writing a lexer, parser, etc for a compiler it all starts to make more and more sense.
The first hurdle I had was realising which function in std.regexp actually did what I wanted, I kept finding myself calling find or match when I really wanted sub or split or something. I'm not sure why but the function names don't immediately 'talk' to me as such.
Regan
|
Copyright © 1999-2021 by the D Language Foundation