tre.nim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import std/re
  2. proc testAll() =
  3. doAssert match("(a b c)", rex"\( .* \)")
  4. doAssert match("WHiLe", re("while", {reIgnoreCase}))
  5. doAssert "0158787".match(re"\d+")
  6. doAssert "ABC 0232".match(re"\w+\s+\d+")
  7. doAssert "ABC".match(rex"\d+ | \w+")
  8. {.push warnings:off.}
  9. doAssert matchLen("key", re"\b[a-zA-Z_]+[a-zA-Z_0-9]*\b") == 3
  10. {.pop.}
  11. var pattern = re"[a-z0-9]+\s*=\s*[a-z0-9]+"
  12. doAssert matchLen("key1= cal9", pattern) == 11
  13. doAssert find("_____abc_______", re"abc") == 5
  14. doAssert findBounds("_____abc_______", re"abc") == (5,7)
  15. var matches: array[6, string]
  16. if match("abcdefg", re"c(d)ef(g)", matches, 2):
  17. doAssert matches[0] == "d"
  18. doAssert matches[1] == "g"
  19. else:
  20. doAssert false
  21. if "abc" =~ re"(a)bcxyz|(\w+)":
  22. doAssert matches[1] == "abc"
  23. else:
  24. doAssert false
  25. if "abc" =~ re"(cba)?.*":
  26. doAssert matches[0] == ""
  27. else: doAssert false
  28. if "abc" =~ re"().*":
  29. doAssert matches[0] == ""
  30. else: doAssert false
  31. doAssert "var1=key; var2=key2".endsWith(re"\w+=\w+")
  32. doAssert("var1=key; var2=key2".replacef(re"(\w+)=(\w+)", "$1<-$2$2") ==
  33. "var1<-keykey; var2<-key2key2")
  34. doAssert("var1=key; var2=key2".replace(re"(\w+)=(\w+)", "$1<-$2$2") ==
  35. "$1<-$2$2; $1<-$2$2")
  36. var accum: seq[string] = @[]
  37. for word in split("00232this02939is39an22example111", re"\d+"):
  38. accum.add(word)
  39. doAssert(accum == @["", "this", "is", "an", "example", ""])
  40. accum = @[]
  41. for word in split("00232this02939is39an22example111", re"\d+", maxsplit=2):
  42. accum.add(word)
  43. doAssert(accum == @["", "this", "is39an22example111"])
  44. accum = @[]
  45. for word in split("AAA : : BBB", re"\s*:\s*"):
  46. accum.add(word)
  47. doAssert(accum == @["AAA", "", "BBB"])
  48. doAssert(split("abc", re"") == @["a", "b", "c"])
  49. doAssert(split("", re"") == @[])
  50. doAssert(split("a;b;c", re";") == @["a", "b", "c"])
  51. doAssert(split(";a;b;c", re";") == @["", "a", "b", "c"])
  52. doAssert(split(";a;b;c;", re";") == @["", "a", "b", "c", ""])
  53. doAssert(split("a;b;c;", re";") == @["a", "b", "c", ""])
  54. doAssert(split("00232this02939is39an22example111", re"\d+", maxsplit=2) == @["", "this", "is39an22example111"])
  55. for x in findAll("abcdef", re"^{.}", 3):
  56. doAssert x == "d"
  57. accum = @[]
  58. for x in findAll("abcdef", re".", 3):
  59. accum.add(x)
  60. doAssert(accum == @["d", "e", "f"])
  61. doAssert("XYZ".find(re"^\d*") == 0)
  62. doAssert("XYZ".match(re"^\d*") == true)
  63. block:
  64. var matches: array[16, string]
  65. if match("abcdefghijklmnop", re"(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)", matches):
  66. for i in 0..matches.high:
  67. doAssert matches[i] == $chr(i + 'a'.ord)
  68. else:
  69. doAssert false
  70. block: # Buffer based RE
  71. var cs: cstring = "_____abc_______"
  72. doAssert(cs.find(re"abc", bufSize=15) == 5)
  73. doAssert(cs.matchLen(re"_*abc", bufSize=15) == 8)
  74. doAssert(cs.matchLen(re"abc", start=5, bufSize=15) == 3)
  75. doAssert(cs.matchLen(re"abc", start=5, bufSize=7) == -1)
  76. doAssert(cs.matchLen(re"abc_*", start=5, bufSize=10) == 5)
  77. var accum: seq[string] = @[]
  78. for x in cs.findAll(re"[a-z]", start=3, bufSize=15):
  79. accum.add($x)
  80. doAssert(accum == @["a","b","c"])
  81. block:
  82. # bug #9306
  83. doAssert replace("bar", re"^", "foo") == "foobar"
  84. doAssert replace("foo", re"", "-") == "-foo"
  85. doAssert replace("foo", re"$", "bar") == "foobar"
  86. testAll()