String & delimit
david
tazz at gmx.at
Wed Apr 4 05:28:30 PDT 2007
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
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: strex.d
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070404/8affb0f0/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.d
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070404/8affb0f0/attachment-0001.ksh>
More information about the Digitalmars-d
mailing list