www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - String & delimit

reply =?ISO-8859-1?Q?Di=e9?= <d.cavadini freesurf.ch> writes:
Hello

I have this string:

blablabla thisis a string example <i_need_this_text>

With Tango.text.Util i think i can use "delimit" function for extracting the
test "i_need_this_text".

delimit("blablabla thisis a string example <i_need_this_text>","<>")

But i found in http://www.dsource.org/projects/tango/docs/current/tango.text.Util.html

the delimit declaration:

T[][] delimit (T)(T[] src, T[] set);

and now how can i correctly use this function with template? I suppose i must
declare e template like this

template TText(T){alias T* t;}

but i dont understand how use the template with the delimit function.

Someone can explain me?
Thank you!
Apr 03 2007
next sibling parent reply Pragma <ericanderton yahoo.removeme.com> writes:
Diť wrote:
 Hello
 
 I have this string:
 
 blablabla thisis a string example <i_need_this_text>
 
 With Tango.text.Util i think i can use "delimit" function for extracting the
test "i_need_this_text".
 
 delimit("blablabla thisis a string example <i_need_this_text>","<>")
 
 But i found in http://www.dsource.org/projects/tango/docs/current/tango.text.Util.html
 
 the delimit declaration:
 
 T[][] delimit (T)(T[] src, T[] set);
 
 and now how can i correctly use this function with template? I suppose i must
declare e template like this
 
 template TText(T){alias T* t;}
 
 but i dont understand how use the template with the delimit function.
 
 Someone can explain me?
 Thank you!
 

First of all, the template uses Inline Function-Template Instantiation (IFTI); the compiler will *deduce* what 'T' is based on the arguments you pass to delimit(). Just call it like "delimit(string1,string2)" and it'll work fine. Second, let's look at the documentation for the delimit() function:
 # T[][] delimit (T)(T[] src, T[] set);
     Split the provided array wherever a delimiter-set instance is found,
 and return the resultant segments. The delimiters are excluded from
 each of the segments. Note that delimiters are matched as a set of
 alternates rather than as a pattern.

Arguably, this is not the best description in the world. It makes much more sense after seeing an example of what it does: auto x = delimit("One,Two;Three.Four&Five",",;.&"); The variable x will now be an array of strings, containing "One","Two","Three","Four", and "Five" in that order. It's important to note that the second argument of the delimit() function is used as a series of single-character delimiters that are used to split up the source string. The delimiters themselves are not returned. -- - EricAnderton at yahoo
Apr 03 2007
next sibling parent =?ISO-8859-1?Q?Di=e9?= <d.cavadini freesurf.ch> writes:
Pragma Wrote:

 Diť wrote:
 Hello
 
 I have this string:
 
 blablabla thisis a string example <i_need_this_text>
 
 With Tango.text.Util i think i can use "delimit" function for extracting the
test "i_need_this_text".
 
 delimit("blablabla thisis a string example <i_need_this_text>","<>")
 
 But i found in http://www.dsource.org/projects/tango/docs/current/tango.text.Util.html
 
 the delimit declaration:
 
 T[][] delimit (T)(T[] src, T[] set);
 
 and now how can i correctly use this function with template? I suppose i must
declare e template like this
 
 template TText(T){alias T* t;}
 
 but i dont understand how use the template with the delimit function.
 
 Someone can explain me?
 Thank you!
 

First of all, the template uses Inline Function-Template Instantiation (IFTI); the compiler will *deduce* what 'T' is based on the arguments you pass to delimit(). Just call it like "delimit(string1,string2)" and it'll work fine. Second, let's look at the documentation for the delimit() function: > # T[][] delimit (T)(T[] src, T[] set); > Split the provided array wherever a delimiter-set instance is found, > and return the resultant segments. The delimiters are excluded from > each of the segments. Note that delimiters are matched as a set of > alternates rather than as a pattern. Arguably, this is not the best description in the world. It makes much more sense after seeing an example of what it does: auto x = delimit("One,Two;Three.Four&Five",",;.&"); The variable x will now be an array of strings, containing "One","Two","Three","Four", and "Five" in that order. It's important to note that the second argument of the delimit() function is used as a series of single-character delimiters that are used to split up the source string. The delimiters themselves are not returned. -- - EricAnderton at yahoo

Thank you Pragma, before posting my problem i have read the doc. and i have tried with "delimit(string1,string2)" (directly with char type) but after your post i think my problem is another.... mmm i wont to discover this new problem now! Thank you, bye
Apr 03 2007
prev sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Pragma wrote:
 Inline Function-Template Instantiation (IFTI)

*Implicit* Function-Template Instantiation.
Apr 03 2007
parent reply Pragma <ericanderton yahoo.removeme.com> writes:
Frits van Bommel wrote:
 Pragma wrote:
 Inline Function-Template Instantiation (IFTI)

*Implicit* Function-Template Instantiation.

D'oh. :( -- - EricAnderton at yahoo
Apr 03 2007
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Pragma wrote:
 Frits van Bommel wrote:
 Pragma wrote:
 Inline Function-Template Instantiation (IFTI)

*Implicit* Function-Template Instantiation.

D'oh. :(

No worries, I think in this case "Inline" makes a reasonable synonym. And its easier to say. ;) -- Chris Nicholson-Sauls
Apr 03 2007
prev sibling next sibling parent reply Gregor Kopp <gk cutcopy.com> writes:
import std.string;
import std.stdio;
void main()
{writefln(split(split(
"blablabla thisis a string example <i_need_this_text> ole!"
, "<")[1],">")[0]);}


SCNR!!!
Maybe a Regex would be better, if you just want to do textprocessing?

in good fun, Gregor
Apr 03 2007
parent reply Dan <murpsoft hotmail.com> writes:
Gregor Kopp Wrote:

 import std.string;
 import std.stdio;
 void main()
 {writefln(split(split(
 "blablabla thisis a string example <i_need_this_text> ole!"
 , "<")[1],">")[0]);}
 SCNR!!!
 Maybe a Regex would be better, if you just want to do textprocessing?
 
 in good fun, Gregor

In good fun?? Good! In JavaScript, I think I write it something like: "blablabla thisis a string example <i_need_this_text> ole!".match( /<([^>]+)>/m)[1]; I haven't done any web data mining for a while now, so I can't remember what [#] to use for first sub-match. : p
Apr 03 2007
parent reply Gregor Kopp <gk cutcopy.com> writes:
Dan schrieb:
 In JavaScript, I think I write it something like:
 
 "blablabla thisis a string example <i_need_this_text> ole!".match(
 /<([^>]+)>/m)[1];
 

I converted your pattern to a small d program: ---begin import std.stdio; import std.regexp; void main() { auto s = "blablabla thisis a string example <i_need_this_text> ole!"; auto m = std.regexp.search(s, "<([^>]+)>"); if (m) writefln("%s", m.match(0)); } ---end which prints <i_need_this_text> I don't have time to take a closer look at your pattern so that it prints only i_need_this_text. I always get headache on Regexp ;) greets, Gregor ps.: have found it at [1] and [2] [1] http://www.digitalmars.com/d/regular-expression.html [2] http://www.digitalmars.com/d/phobos/std_regexp.html
Apr 04 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Gregor Kopp wrote:
 ---begin
 import std.stdio;
 import std.regexp;
 void main()
 {
 auto s = "blablabla thisis a string example <i_need_this_text> ole!";
 auto m = std.regexp.search(s, "<([^>]+)>");
 if (m) writefln("%s", m.match(0));
 }
 ---end
 
 which prints <i_need_this_text>
 
 I don't have time to take a closer look at your pattern so that it 
 prints only i_need_this_text. I always get headache on Regexp ;)

It's quite easy, in fact it's a one-character patch: if (m) writefln("%s", m.match(1)); A parameter > 0 passed to match(int) returns the part that matches the corresponding pair of parentheses, and the correct subexpression was already parenthesized...
Apr 04 2007
parent Gregor Kopp <gk cutcopy.com> writes:
Frits van Bommel schrieb:
 It's quite easy, in fact it's a one-character patch:
     if (m) writefln("%s", m.match(1));
 
 A parameter > 0 passed to match(int) returns the part that matches the 
 corresponding pair of parentheses, and the correct subexpression was 
 already parenthesized...

Thank you for this info! Maybe this all helps Dié with his problem. Lets hope so ;)
Apr 04 2007
prev sibling parent david <tazz gmx.at> writes:
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 8bit

Diť schrieb:
 Hello
 
 I have this string:
 
 blablabla thisis a string example <i_need_this_text>
 

Hello Diť, did you get it worked out? If not: import std.regexp; int main(char[][] args) { char[] message = "just <a> few <tags>, no <nesting here>"; RegExp rg = new RegExp("<.*>", "g"); char[][] res = rg.match(message); assert(res[0] == "<a>"); assert(res[1] == "<tags>"); assert(res[2] == "<nesting here>"); // remove the delimiters foreach(inout v; res) v = v[1..$-1]; assert(res[0] == "a"); assert(res[1] == "tags"); assert(res[2] == "nesting here"); } If you need nesting (is this possible with RexExp? Found nothing...) you can use the function char[][] findNesting(char[] string, char left, char right) in the file (strex.d) I attached, which splits "this <test> is <advanced, as <you can> see>a<b<c>d>><e" in "test" "advanced, as <you can> see" "you can" "b<c>d" "c" (see test.d for examples) If you want correct nesting but only the innermost tags, apply the function bool containsTag(char[] string, char left, char right) also in the attached strex.d in combination with char[][] findNesting(char[] string, char left, char right) david
Apr 04 2007