www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Regexp

reply okibi <okibi ratedo.com> writes:
I've got another question regarding regexp:


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!
Jul 12 2007
parent reply Regan Heath <regan netmail.co.nz> writes:
okibi wrote:
 I've got another question regarding regexp:
 

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. 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 ( 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 replace it with the format "<var>$1</var>" I tested this with: import std.regexp, std.stdio; string format = r"<var>$1</var>"; void main() { writefln(std.regexp.sub(document, pattern, format, "gi")); } Seems to work. :) Regan
Jul 12 2007
parent reply okibi <okibi ratedo.com> writes:
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:
 

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. 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 ( 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 replace it with the format "<var>$1</var>" I tested this with: import std.regexp, std.stdio; string format = r"<var>$1</var>"; void main() { writefln(std.regexp.sub(document, pattern, format, "gi")); } Seems to work. :) Regan
Jul 12 2007
parent Regan Heath <regan netmail.co.nz> writes:
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
Jul 12 2007