1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 /* 25 * Conversion parameters: 26 * inFile = glib-Perl-compatible-regular-expressions.html 27 * outPack = glib 28 * outFile = Regex 29 * strct = GRegex 30 * realStrct= 31 * ctorStrct= 32 * clss = Regex 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_regex_ 41 * omit structs: 42 * omit prefixes: 43 * - g_match_info_ 44 * omit code: 45 * omit signals: 46 * imports: 47 * - glib.Str 48 * - glib.ErrorG 49 * - glib.GException 50 * - glib.MatchInfo 51 * structWrap: 52 * - GMatchInfo* -> MatchInfo 53 * - GRegex* -> Regex 54 * module aliases: 55 * local aliases: 56 * overrides: 57 */ 58 59 module glib.Regex; 60 61 public import gtkc.glibtypes; 62 63 private import gtkc.glib; 64 private import glib.ConstructionException; 65 66 67 private import glib.Str; 68 private import glib.ErrorG; 69 private import glib.GException; 70 private import glib.MatchInfo; 71 72 73 74 75 /** 76 * Description 77 * The g_regex_*() functions implement regular 78 * expression pattern matching using syntax and semantics similar to 79 * Perl regular expression. 80 * Some functions accept a start_position argument, setting it differs 81 * from just passing over a shortened string and setting G_REGEX_MATCH_NOTBOL 82 * in the case of a pattern that begins with any kind of lookbehind assertion. 83 * For example, consider the pattern "\Biss\B" which finds occurrences of "iss" 84 * in the middle of words. ("\B" matches only if the current position in the 85 * subject is not a word boundary.) When applied to the string "Mississipi" 86 * from the fourth byte, namely "issipi", it does not match, because "\B" is 87 * always false at the start of the subject, which is deemed to be a word 88 * boundary. However, if the entire string is passed , but with 89 * start_position set to 4, it finds the second occurrence of "iss" because 90 * it is able to look behind the starting point to discover that it is 91 * preceded by a letter. 92 * Note that, unless you set the G_REGEX_RAW flag, all the strings passed 93 * to these functions must be encoded in UTF-8. The lengths and the positions 94 * inside the strings are in bytes and not in characters, so, for instance, 95 * "\xc3\xa0" (i.e. "à") is two bytes long but it is treated as a 96 * single character. If you set G_REGEX_RAW the strings can be non-valid 97 * UTF-8 strings and a byte is treated as a character, so "\xc3\xa0" is two 98 * bytes and two characters long. 99 * When matching a pattern, "\n" matches only against a "\n" character in 100 * the string, and "\r" matches only a "\r" character. To match any newline 101 * sequence use "\R". This particular group matches either the two-character 102 * sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed, 103 * U+000A, "\n"), VT vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"), 104 * CR (carriage return, U+000D, "\r"), NEL (next line, U+0085), LS (line 105 * separator, U+2028), or PS (paragraph separator, U+2029). 106 * The behaviour of the dot, circumflex, and dollar metacharacters are 107 * affected by newline characters, the default is to recognize any newline 108 * character (the same characters recognized by "\R"). This can be changed 109 * with G_REGEX_NEWLINE_CR, G_REGEX_NEWLINE_LF and G_REGEX_NEWLINE_CRLF 110 * compile options, and with G_REGEX_MATCH_NEWLINE_ANY, 111 * G_REGEX_MATCH_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF and 112 * G_REGEX_MATCH_NEWLINE_CRLF match options. These settings are also 113 * relevant when compiling a pattern if G_REGEX_EXTENDED is set, and an 114 * unescaped "#" outside a character class is encountered. This indicates 115 * a comment that lasts until after the next newline. 116 * Creating and manipulating the same GRegex structure from different 117 * threads is not a problem as GRegex does not modify its internal 118 * state between creation and destruction, on the other hand GMatchInfo 119 * is not threadsafe. 120 * The regular expressions low-level functionalities are obtained through 121 * the excellent PCRE library 122 * written by Philip Hazel. 123 */ 124 public class Regex 125 { 126 127 /** the main Gtk struct */ 128 protected GRegex* gRegex; 129 130 131 public GRegex* getRegexStruct() 132 { 133 return gRegex; 134 } 135 136 137 /** the main Gtk struct as a void* */ 138 protected void* getStruct() 139 { 140 return cast(void*)gRegex; 141 } 142 143 /** 144 * Sets our main struct and passes it to the parent class 145 */ 146 public this (GRegex* gRegex) 147 { 148 this.gRegex = gRegex; 149 } 150 151 /** 152 */ 153 154 /** 155 * Compiles the regular expression to an internal form, and does 156 * the initial setup of the GRegex structure. 157 * Since 2.14 158 * Params: 159 * pattern = the regular expression 160 * compileOptions = compile options for the regular expression, or 0 161 * matchOptions = match options for the regular expression, or 0 162 * Throws: GException on failure. 163 * Throws: ConstructionException GTK+ fails to create the object. 164 */ 165 public this (string pattern, GRegexCompileFlags compileOptions, GRegexMatchFlags matchOptions) 166 { 167 // GRegex * g_regex_new (const gchar *pattern, GRegexCompileFlags compile_options, GRegexMatchFlags match_options, GError **error); 168 GError* err = null; 169 170 auto p = g_regex_new(Str.toStringz(pattern), compileOptions, matchOptions, &err); 171 172 if (err !is null) 173 { 174 throw new GException( new ErrorG(err) ); 175 } 176 177 if(p is null) 178 { 179 throw new ConstructionException("null returned by g_regex_new(Str.toStringz(pattern), compileOptions, matchOptions, &err)"); 180 } 181 this(cast(GRegex*) p); 182 } 183 184 /** 185 * Increases reference count of regex by 1. 186 * Since 2.14 187 * Returns: regex 188 */ 189 public Regex doref() 190 { 191 // GRegex * g_regex_ref (GRegex *regex); 192 auto p = g_regex_ref(gRegex); 193 194 if(p is null) 195 { 196 return null; 197 } 198 199 return new Regex(cast(GRegex*) p); 200 } 201 202 /** 203 * Decreases reference count of regex by 1. When reference count drops 204 * to zero, it frees all the memory associated with the regex structure. 205 * Since 2.14 206 */ 207 public void unref() 208 { 209 // void g_regex_unref (GRegex *regex); 210 g_regex_unref(gRegex); 211 } 212 213 /** 214 * Gets the pattern string associated with regex, i.e. a copy of 215 * the string passed to g_regex_new(). 216 * Since 2.14 217 * Returns: the pattern of regex 218 */ 219 public string getPattern() 220 { 221 // const gchar * g_regex_get_pattern (const GRegex *regex); 222 return Str.toString(g_regex_get_pattern(gRegex)); 223 } 224 225 /** 226 * Returns the number of the highest back reference 227 * in the pattern, or 0 if the pattern does not contain 228 * back references. 229 * Since 2.14 230 * Returns: the number of the highest back reference 231 */ 232 public int getMaxBackref() 233 { 234 // gint g_regex_get_max_backref (const GRegex *regex); 235 return g_regex_get_max_backref(gRegex); 236 } 237 238 /** 239 * Returns the number of capturing subpatterns in the pattern. 240 * Since 2.14 241 * Returns: the number of capturing subpatterns 242 */ 243 public int getCaptureCount() 244 { 245 // gint g_regex_get_capture_count (const GRegex *regex); 246 return g_regex_get_capture_count(gRegex); 247 } 248 249 /** 250 * Retrieves the number of the subexpression named name. 251 * Since 2.14 252 * Params: 253 * name = name of the subexpression 254 * Returns: The number of the subexpression or -1 if name does not exists 255 */ 256 public int getStringNumber(string name) 257 { 258 // gint g_regex_get_string_number (const GRegex *regex, const gchar *name); 259 return g_regex_get_string_number(gRegex, Str.toStringz(name)); 260 } 261 262 /** 263 * Returns the compile options that regex was created with. 264 * Since 2.26 265 * Returns: flags from GRegexCompileFlags 266 */ 267 public GRegexCompileFlags getCompileFlags() 268 { 269 // GRegexCompileFlags g_regex_get_compile_flags (const GRegex *regex); 270 return g_regex_get_compile_flags(gRegex); 271 } 272 273 /** 274 * Returns the match options that regex was created with. 275 * Since 2.26 276 * Returns: flags from GRegexMatchFlags 277 */ 278 public GRegexMatchFlags getMatchFlags() 279 { 280 // GRegexMatchFlags g_regex_get_match_flags (const GRegex *regex); 281 return g_regex_get_match_flags(gRegex); 282 } 283 284 /** 285 * Escapes the special characters used for regular expressions 286 * in string, for instance "a.b*c" becomes "a\.b\*c". This 287 * function is useful to dynamically generate regular expressions. 288 * string can contain nul characters that are replaced with "\0", 289 * in this case remember to specify the correct length of string 290 * in length. 291 * Since 2.14 292 * Params: 293 * string = the string to escape. [array length=length] 294 * length = the length of string, or -1 if string is nul-terminated 295 * Returns: a newly-allocated escaped string 296 */ 297 public static string escapeString(string string, int length) 298 { 299 // gchar * g_regex_escape_string (const gchar *string, gint length); 300 return Str.toString(g_regex_escape_string(Str.toStringz(string), length)); 301 } 302 303 /** 304 * Scans for a match in string for pattern. 305 * This function is equivalent to g_regex_match() but it does not 306 * require to compile the pattern with g_regex_new(), avoiding some 307 * lines of code when you need just to do a match without extracting 308 * substrings, capture counts, and so on. 309 * If this function is to be called on the same pattern more than 310 * once, it's more efficient to compile the pattern once with 311 * g_regex_new() and then use g_regex_match(). 312 * Since 2.14 313 * Params: 314 * pattern = the regular expression 315 * string = the string to scan for matches 316 * compileOptions = compile options for the regular expression, or 0 317 * matchOptions = match options, or 0 318 * Returns: TRUE if the string matched, FALSE otherwise 319 */ 320 public static int matchSimple(string pattern, string string, GRegexCompileFlags compileOptions, GRegexMatchFlags matchOptions) 321 { 322 // gboolean g_regex_match_simple (const gchar *pattern, const gchar *string, GRegexCompileFlags compile_options, GRegexMatchFlags match_options); 323 return g_regex_match_simple(Str.toStringz(pattern), Str.toStringz(string), compileOptions, matchOptions); 324 } 325 326 /** 327 * Scans for a match in string for the pattern in regex. 328 * The match_options are combined with the match options specified 329 * when the regex structure was created, letting you have more 330 * flexibility in reusing GRegex structures. 331 * A GMatchInfo structure, used to get information on the match, 332 * is stored in match_info if not NULL. Note that if match_info 333 * is not NULL then it is created even if the function returns FALSE, 334 * i.e. you must free it regardless if regular expression actually matched. 335 * To retrieve all the non-overlapping matches of the pattern in 336 * string you can use g_match_info_next(). 337 * $(DDOC_COMMENT example) 338 * string is not copied and is used in GMatchInfo internally. If 339 * you use any GMatchInfo method (except g_match_info_free()) after 340 * freeing or modifying string then the behaviour is undefined. 341 * Since 2.14 342 * Params: 343 * string = the string to scan for matches 344 * matchOptions = match options 345 * matchInfo = pointer to location where to store 346 * the GMatchInfo, or NULL if you do not need it. [out][allow-none] 347 * Returns: TRUE is the string matched, FALSE otherwise 348 */ 349 public int match(string string, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 350 { 351 // gboolean g_regex_match (const GRegex *regex, const gchar *string, GRegexMatchFlags match_options, GMatchInfo **match_info); 352 GMatchInfo* outmatchInfo = null; 353 354 auto p = g_regex_match(gRegex, Str.toStringz(string), matchOptions, &outmatchInfo); 355 356 matchInfo = new MatchInfo(outmatchInfo); 357 return p; 358 } 359 360 /** 361 * Scans for a match in string for the pattern in regex. 362 * The match_options are combined with the match options specified 363 * when the regex structure was created, letting you have more 364 * flexibility in reusing GRegex structures. 365 * Setting start_position differs from just passing over a shortened 366 * string and setting G_REGEX_MATCH_NOTBOL in the case of a pattern 367 * that begins with any kind of lookbehind assertion, such as "\b". 368 * A GMatchInfo structure, used to get information on the match, is 369 * stored in match_info if not NULL. Note that if match_info is 370 * not NULL then it is created even if the function returns FALSE, 371 * i.e. you must free it regardless if regular expression actually 372 * matched. 373 * string is not copied and is used in GMatchInfo internally. If 374 * you use any GMatchInfo method (except g_match_info_free()) after 375 * freeing or modifying string then the behaviour is undefined. 376 * To retrieve all the non-overlapping matches of the pattern in 377 * string you can use g_match_info_next(). 378 * $(DDOC_COMMENT example) 379 * Since 2.14 380 * Params: 381 * string = the string to scan for matches. [array length=string_len] 382 * stringLen = the length of string, or -1 if string is nul-terminated 383 * startPosition = starting index of the string to match 384 * matchOptions = match options 385 * matchInfo = pointer to location where to store 386 * the GMatchInfo, or NULL if you do not need it. [out][allow-none] 387 * Returns: TRUE is the string matched, FALSE otherwise 388 * Throws: GException on failure. 389 */ 390 public int matchFull(string string, gssize stringLen, int startPosition, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 391 { 392 // gboolean g_regex_match_full (const GRegex *regex, const gchar *string, gssize string_len, gint start_position, GRegexMatchFlags match_options, GMatchInfo **match_info, GError **error); 393 GMatchInfo* outmatchInfo = null; 394 GError* err = null; 395 396 auto p = g_regex_match_full(gRegex, Str.toStringz(string), stringLen, startPosition, matchOptions, &outmatchInfo, &err); 397 398 if (err !is null) 399 { 400 throw new GException( new ErrorG(err) ); 401 } 402 403 matchInfo = new MatchInfo(outmatchInfo); 404 return p; 405 } 406 407 /** 408 * Using the standard algorithm for regular expression matching only 409 * the longest match in the string is retrieved. This function uses 410 * a different algorithm so it can retrieve all the possible matches. 411 * For more documentation see g_regex_match_all_full(). 412 * A GMatchInfo structure, used to get information on the match, is 413 * stored in match_info if not NULL. Note that if match_info is 414 * not NULL then it is created even if the function returns FALSE, 415 * i.e. you must free it regardless if regular expression actually 416 * matched. 417 * string is not copied and is used in GMatchInfo internally. If 418 * you use any GMatchInfo method (except g_match_info_free()) after 419 * freeing or modifying string then the behaviour is undefined. 420 * Since 2.14 421 * Params: 422 * string = the string to scan for matches 423 * matchOptions = match options 424 * matchInfo = pointer to location where to store 425 * the GMatchInfo, or NULL if you do not need it. [out][allow-none] 426 * Returns: TRUE is the string matched, FALSE otherwise 427 */ 428 public int matchAll(string string, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 429 { 430 // gboolean g_regex_match_all (const GRegex *regex, const gchar *string, GRegexMatchFlags match_options, GMatchInfo **match_info); 431 GMatchInfo* outmatchInfo = null; 432 433 auto p = g_regex_match_all(gRegex, Str.toStringz(string), matchOptions, &outmatchInfo); 434 435 matchInfo = new MatchInfo(outmatchInfo); 436 return p; 437 } 438 439 /** 440 * Using the standard algorithm for regular expression matching only 441 * the longest match in the string is retrieved, it is not possibile 442 * to obtain all the available matches. For instance matching 443 * "<a> <b> <c>" against the pattern "<.*>" 444 * you get "<a> <b> <c>". 445 * This function uses a different algorithm (called DFA, i.e. deterministic 446 * finite automaton), so it can retrieve all the possible matches, all 447 * starting at the same point in the string. For instance matching 448 * "<a> <b> <c>" against the pattern "<.*>" 449 * you would obtain three matches: "<a> <b> <c>", 450 * "<a> <b>" and "<a>". 451 * The number of matched strings is retrieved using 452 * g_match_info_get_match_count(). To obtain the matched strings and 453 * their position you can use, respectively, g_match_info_fetch() and 454 * g_match_info_fetch_pos(). Note that the strings are returned in 455 * reverse order of length; that is, the longest matching string is 456 * given first. 457 * Note that the DFA algorithm is slower than the standard one and it 458 * is not able to capture substrings, so backreferences do not work. 459 * Setting start_position differs from just passing over a shortened 460 * string and setting G_REGEX_MATCH_NOTBOL in the case of a pattern 461 * that begins with any kind of lookbehind assertion, such as "\b". 462 * A GMatchInfo structure, used to get information on the match, is 463 * stored in match_info if not NULL. Note that if match_info is 464 * not NULL then it is created even if the function returns FALSE, 465 * i.e. you must free it regardless if regular expression actually 466 * matched. 467 * string is not copied and is used in GMatchInfo internally. If 468 * you use any GMatchInfo method (except g_match_info_free()) after 469 * freeing or modifying string then the behaviour is undefined. 470 * Since 2.14 471 * Params: 472 * string = the string to scan for matches. [array length=string_len] 473 * stringLen = the length of string, or -1 if string is nul-terminated 474 * startPosition = starting index of the string to match 475 * matchOptions = match options 476 * matchInfo = pointer to location where to store 477 * the GMatchInfo, or NULL if you do not need it. [out][allow-none] 478 * Returns: TRUE is the string matched, FALSE otherwise 479 * Throws: GException on failure. 480 */ 481 public int matchAllFull(string string, gssize stringLen, int startPosition, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 482 { 483 // gboolean g_regex_match_all_full (const GRegex *regex, const gchar *string, gssize string_len, gint start_position, GRegexMatchFlags match_options, GMatchInfo **match_info, GError **error); 484 GMatchInfo* outmatchInfo = null; 485 GError* err = null; 486 487 auto p = g_regex_match_all_full(gRegex, Str.toStringz(string), stringLen, startPosition, matchOptions, &outmatchInfo, &err); 488 489 if (err !is null) 490 { 491 throw new GException( new ErrorG(err) ); 492 } 493 494 matchInfo = new MatchInfo(outmatchInfo); 495 return p; 496 } 497 498 /** 499 * Breaks the string on the pattern, and returns an array of 500 * the tokens. If the pattern contains capturing parentheses, 501 * then the text for each of the substrings will also be returned. 502 * If the pattern does not match anywhere in the string, then the 503 * whole string is returned as the first token. 504 * This function is equivalent to g_regex_split() but it does 505 * not require to compile the pattern with g_regex_new(), avoiding 506 * some lines of code when you need just to do a split without 507 * extracting substrings, capture counts, and so on. 508 * If this function is to be called on the same pattern more than 509 * once, it's more efficient to compile the pattern once with 510 * g_regex_new() and then use g_regex_split(). 511 * As a special case, the result of splitting the empty string "" 512 * is an empty vector, not a vector containing a single string. 513 * The reason for this special case is that being able to represent 514 * a empty vector is typically more useful than consistent handling 515 * of empty elements. If you do need to represent empty elements, 516 * you'll need to check for the empty string before calling this 517 * function. 518 * A pattern that can match empty strings splits string into 519 * separate characters wherever it matches the empty string between 520 * characters. For example splitting "ab c" using as a separator 521 * "\s*", you will get "a", "b" and "c". 522 * Since 2.14 523 * Params: 524 * pattern = the regular expression 525 * string = the string to scan for matches 526 * compileOptions = compile options for the regular expression, or 0 527 * matchOptions = match options, or 0 528 * Returns: a NULL-terminated array of strings. Free it using g_strfreev() 529 */ 530 public static string[] splitSimple(string pattern, string string, GRegexCompileFlags compileOptions, GRegexMatchFlags matchOptions) 531 { 532 // gchar ** g_regex_split_simple (const gchar *pattern, const gchar *string, GRegexCompileFlags compile_options, GRegexMatchFlags match_options); 533 return Str.toStringArray(g_regex_split_simple(Str.toStringz(pattern), Str.toStringz(string), compileOptions, matchOptions)); 534 } 535 536 /** 537 * Breaks the string on the pattern, and returns an array of the tokens. 538 * If the pattern contains capturing parentheses, then the text for each 539 * of the substrings will also be returned. If the pattern does not match 540 * anywhere in the string, then the whole string is returned as the first 541 * token. 542 * As a special case, the result of splitting the empty string "" is an 543 * empty vector, not a vector containing a single string. The reason for 544 * this special case is that being able to represent a empty vector is 545 * typically more useful than consistent handling of empty elements. If 546 * you do need to represent empty elements, you'll need to check for the 547 * empty string before calling this function. 548 * A pattern that can match empty strings splits string into separate 549 * characters wherever it matches the empty string between characters. 550 * For example splitting "ab c" using as a separator "\s*", you will get 551 * "a", "b" and "c". 552 * Since 2.14 553 * Params: 554 * string = the string to split with the pattern 555 * matchOptions = match time option flags 556 * Returns: a NULL-terminated gchar ** array. Free it using g_strfreev() 557 */ 558 public string[] split(string string, GRegexMatchFlags matchOptions) 559 { 560 // gchar ** g_regex_split (const GRegex *regex, const gchar *string, GRegexMatchFlags match_options); 561 return Str.toStringArray(g_regex_split(gRegex, Str.toStringz(string), matchOptions)); 562 } 563 564 /** 565 * Breaks the string on the pattern, and returns an array of the tokens. 566 * If the pattern contains capturing parentheses, then the text for each 567 * of the substrings will also be returned. If the pattern does not match 568 * anywhere in the string, then the whole string is returned as the first 569 * token. 570 * As a special case, the result of splitting the empty string "" is an 571 * empty vector, not a vector containing a single string. The reason for 572 * this special case is that being able to represent a empty vector is 573 * typically more useful than consistent handling of empty elements. If 574 * you do need to represent empty elements, you'll need to check for the 575 * empty string before calling this function. 576 * A pattern that can match empty strings splits string into separate 577 * characters wherever it matches the empty string between characters. 578 * For example splitting "ab c" using as a separator "\s*", you will get 579 * "a", "b" and "c". 580 * Setting start_position differs from just passing over a shortened 581 * string and setting G_REGEX_MATCH_NOTBOL in the case of a pattern 582 * that begins with any kind of lookbehind assertion, such as "\b". 583 * Since 2.14 584 * Params: 585 * string = the string to split with the pattern. [array length=string_len] 586 * stringLen = the length of string, or -1 if string is nul-terminated 587 * startPosition = starting index of the string to match 588 * matchOptions = match time option flags 589 * maxTokens = the maximum number of tokens to split string into. 590 * If this is less than 1, the string is split completely 591 * Returns: a NULL-terminated gchar ** array. Free it using g_strfreev() 592 * Throws: GException on failure. 593 */ 594 public string[] splitFull(string string, gssize stringLen, int startPosition, GRegexMatchFlags matchOptions, int maxTokens) 595 { 596 // gchar ** g_regex_split_full (const GRegex *regex, const gchar *string, gssize string_len, gint start_position, GRegexMatchFlags match_options, gint max_tokens, GError **error); 597 GError* err = null; 598 599 auto p = g_regex_split_full(gRegex, Str.toStringz(string), stringLen, startPosition, matchOptions, maxTokens, &err); 600 601 if (err !is null) 602 { 603 throw new GException( new ErrorG(err) ); 604 } 605 606 return Str.toStringArray(p); 607 } 608 609 /** 610 * Replaces all occurrences of the pattern in regex with the 611 * replacement text. Backreferences of the form '\number' or 612 * '\g<number>' in the replacement text are interpolated by the 613 * number-th captured subexpression of the match, '\g<name>' refers 614 * to the captured subexpression with the given name. '\0' refers to the 615 * complete match, but '\0' followed by a number is the octal representation 616 * of a character. To include a literal '\' in the replacement, write '\\'. 617 * Since 2.14 618 * Params: 619 * string = the string to perform matches against. [array length=string_len] 620 * stringLen = the length of string, or -1 if string is nul-terminated 621 * startPosition = starting index of the string to match 622 * replacement = text to replace each match with 623 * matchOptions = options for the match 624 * Returns: a newly allocated string containing the replacements 625 * Throws: GException on failure. 626 */ 627 public string replace(string string, gssize stringLen, int startPosition, string replacement, GRegexMatchFlags matchOptions) 628 { 629 // gchar * g_regex_replace (const GRegex *regex, const gchar *string, gssize string_len, gint start_position, const gchar *replacement, GRegexMatchFlags match_options, GError **error); 630 GError* err = null; 631 632 auto p = g_regex_replace(gRegex, Str.toStringz(string), stringLen, startPosition, Str.toStringz(replacement), matchOptions, &err); 633 634 if (err !is null) 635 { 636 throw new GException( new ErrorG(err) ); 637 } 638 639 return Str.toString(p); 640 } 641 642 /** 643 * Replaces all occurrences of the pattern in regex with the 644 * replacement text. replacement is replaced literally, to 645 * include backreferences use g_regex_replace(). 646 * Setting start_position differs from just passing over a 647 * shortened string and setting G_REGEX_MATCH_NOTBOL in the 648 * case of a pattern that begins with any kind of lookbehind 649 * assertion, such as "\b". 650 * Since 2.14 651 * Params: 652 * string = the string to perform matches against. [array length=string_len] 653 * stringLen = the length of string, or -1 if string is nul-terminated 654 * startPosition = starting index of the string to match 655 * replacement = text to replace each match with 656 * matchOptions = options for the match 657 * Returns: a newly allocated string containing the replacements 658 * Throws: GException on failure. 659 */ 660 public string replaceLiteral(string string, gssize stringLen, int startPosition, string replacement, GRegexMatchFlags matchOptions) 661 { 662 // gchar * g_regex_replace_literal (const GRegex *regex, const gchar *string, gssize string_len, gint start_position, const gchar *replacement, GRegexMatchFlags match_options, GError **error); 663 GError* err = null; 664 665 auto p = g_regex_replace_literal(gRegex, Str.toStringz(string), stringLen, startPosition, Str.toStringz(replacement), matchOptions, &err); 666 667 if (err !is null) 668 { 669 throw new GException( new ErrorG(err) ); 670 } 671 672 return Str.toString(p); 673 } 674 675 /** 676 * Replaces occurrences of the pattern in regex with the output of 677 * eval for that occurrence. 678 * Setting start_position differs from just passing over a shortened 679 * string and setting G_REGEX_MATCH_NOTBOL in the case of a pattern 680 * that begins with any kind of lookbehind assertion, such as "\b". 681 * The following example uses g_regex_replace_eval() to replace multiple 682 * Since 2.14 683 * Params: 684 * string = string to perform matches against. [array length=string_len] 685 * stringLen = the length of string, or -1 if string is nul-terminated 686 * startPosition = starting index of the string to match 687 * matchOptions = options for the match 688 * eval = a function to call for each match 689 * userData = user data to pass to the function 690 * Returns: a newly allocated string containing the replacements 691 * Throws: GException on failure. 692 */ 693 public string replaceEval(string string, gssize stringLen, int startPosition, GRegexMatchFlags matchOptions, GRegexEvalCallback eval, void* userData) 694 { 695 // gchar * g_regex_replace_eval (const GRegex *regex, const gchar *string, gssize string_len, gint start_position, GRegexMatchFlags match_options, GRegexEvalCallback eval, gpointer user_data, GError **error); 696 GError* err = null; 697 698 auto p = g_regex_replace_eval(gRegex, Str.toStringz(string), stringLen, startPosition, matchOptions, eval, userData, &err); 699 700 if (err !is null) 701 { 702 throw new GException( new ErrorG(err) ); 703 } 704 705 return Str.toString(p); 706 } 707 708 /** 709 * Checks whether replacement is a valid replacement string 710 * (see g_regex_replace()), i.e. that all escape sequences in 711 * it are valid. 712 * If has_references is not NULL then replacement is checked 713 * for pattern references. For instance, replacement text 'foo\n' 714 * does not contain references and may be evaluated without information 715 * about actual match, but '\0\1' (whole match followed by first 716 * subpattern) requires valid GMatchInfo object. 717 * Since 2.14 718 * Params: 719 * replacement = the replacement string 720 * hasReferences = location to store information about 721 * references in replacement or NULL. [out][allow-none] 722 * Returns: whether replacement is a valid replacement string 723 * Throws: GException on failure. 724 */ 725 public static int checkReplacement(string replacement, out int hasReferences) 726 { 727 // gboolean g_regex_check_replacement (const gchar *replacement, gboolean *has_references, GError **error); 728 GError* err = null; 729 730 auto p = g_regex_check_replacement(Str.toStringz(replacement), &hasReferences, &err); 731 732 if (err !is null) 733 { 734 throw new GException( new ErrorG(err) ); 735 } 736 737 return p; 738 } 739 }