regexp.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. #include <sys/types.h>
  3. #include <util/system/defaults.h>
  4. #include <util/generic/string.h>
  5. #include <util/generic/yexception.h>
  6. #include <contrib/libs/pcre/pcre.h>
  7. #include <contrib/libs/pcre/pcreposix.h>
  8. //THIS CODE LOOKS LIKE A TRASH, BUT WORKS.
  9. #define NMATCHES 100
  10. #define REGEXP_GLOBAL 0x0080 // use this if you want to find all occurences
  11. class TRegExBaseImpl;
  12. class TRegExBase {
  13. protected:
  14. TSimpleIntrusivePtr<TRegExBaseImpl> Impl;
  15. public:
  16. TRegExBase(const char* regExpr = nullptr, int cflags = REG_EXTENDED);
  17. TRegExBase(const TString& regExpr, int cflags = REG_EXTENDED);
  18. virtual ~TRegExBase();
  19. int Exec(const char* str, regmatch_t pmatch[], int eflags, int nmatches = NMATCHES) const;
  20. void Compile(const TString& regExpr, int cflags = REG_EXTENDED);
  21. bool IsCompiled() const;
  22. int GetCompileOptions() const;
  23. TString GetRegExpr() const;
  24. };
  25. class TRegExMatch: public TRegExBase {
  26. public:
  27. TRegExMatch(const char* regExpr = nullptr, int cflags = REG_NOSUB | REG_EXTENDED);
  28. TRegExMatch(const TString& regExpr, int cflags = REG_NOSUB | REG_EXTENDED);
  29. bool Match(const char* str) const;
  30. };
  31. struct TBackReferences {
  32. int Beg;
  33. int End;
  34. int Refer;
  35. };
  36. class TRegExSubst: public TRegExBase {
  37. private:
  38. const char* Replacement;
  39. regmatch_t PMatch[NMATCHES];
  40. TBackReferences Brfs[NMATCHES];
  41. int BrfsCount;
  42. public:
  43. TRegExSubst(const char* regExpr = nullptr, int cflags = REG_EXTENDED);
  44. TString Replace(const char* str, int eflags = 0);
  45. int ParseReplacement(const char* replacement);
  46. };