Giter VIP home page Giter VIP logo

pawn.regex's Introduction

Pawn.Regex

๐Ÿ”Ž Plugin that adds support for regular expressions in Pawn

Why is it better than others plugins? Because it gives you an opportunity to get match groups.

Natives

native Regex:Regex_New(const pattern[], E_REGEX_FLAG:flags = REGEX_DEFAULT, E_REGEX_GRAMMAR:grammar = REGEX_ECMASCRIPT);
native Regex_Delete(&Regex:r);

native Regex_Check(const str[], Regex:r, E_MATCH_FLAG:flags = MATCH_DEFAULT);
native Regex_Match(const str[], Regex:r, &RegexMatch:m, E_MATCH_FLAG:flags = MATCH_DEFAULT);
native Regex_Search(const str[], Regex:r, &RegexMatch:m, &pos, startpos = 0, E_MATCH_FLAG:flags = MATCH_DEFAULT);
native Regex_Replace(const str[], Regex:r, const fmt[], dest[], E_MATCH_FLAG:flags = MATCH_DEFAULT, size = sizeof dest);

native Match_GetGroup(RegexMatch:m, index, dest[], &length, size = sizeof dest);
native Match_Free(&RegexMatch:m);

Examples

#include <Pawn.Regex>

stock IsRpNickname(const nickname[])
{
  static Regex:regex;
  if (!regex) regex = Regex_New("[A-Z][a-z]+_[A-Z][a-z]+");

  return Regex_Check(nickname, regex);
}

stock IsValidEmail(const email[])
{
  static Regex:regex;
  if (!regex) regex = Regex_New("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$");

  return Regex_Check(email, regex);
}

public OnPlayerCommandText(playerid, cmdtext[])
{
  static Regex:regex;
  if (!regex) regex = Regex_New("^\\/([\\w]+)\\s*(.+?)?\\s*$");

  new RegexMatch:match;
  if (!Regex_Match(cmdtext, regex, match)) return 0;

  new cmd[16], cmd_length;
  Match_GetGroup(match, 1, cmd, cmd_length);

  new params[256], params_length;
  Match_GetGroup(match, 2, params, params_length);

  printf("cmd '%s' (len %d), params '%s' (len %d)", cmd, cmd_length, params, params_length);

  Match_Free(match);

  return 1;
}

stock SplitAndPrint(const str[])
{
  static Regex:regex;
  if (!regex) regex = Regex_New("[^\\s]+");

  new RegexMatch:match, pos, startpos;
  while (Regex_Search(str, regex, match, pos, startpos))
  {
    new word[128], length;
    Match_GetGroup(match, 0, word, length);

    printf("word: %s, len: %d", word, length);

    startpos += pos + length;

    Match_Free(match);
  }
}

stock ReplaceString(const str[], const regexp[], const fmt[], dest[], size = sizeof dest)
{
  new Regex:regex = Regex_New(regexp);
  if (!regex) return;

  Regex_Replace(str, regex, fmt, dest, MATCH_DEFAULT, size);

  Regex_Delete(regex);
}

main()
{
  new str[256];

  ReplaceString("Regex.Pawn", "(.+)\\.(.+)", "$1.$2 => $2.$1", str);

  printf("%s", str);

  SplitAndPrint("4 8 15 16 23 42");

  OnPlayerCommandText(-1, "/ban 42");
  OnPlayerCommandText(-1, "/kill");

  printf("%d %d %d %d", IsRpNickname("Firstname_Lastname"), IsRpNickname("katursis"), IsValidEmail("[email protected]"), IsValidEmail("email.example.com"));
}

pawn.regex's People

Contributors

dayvison avatar katursis avatar y-less avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pawn.regex's Issues

[Pawn.Regex] Plugin::n_regex_replace: invalid str or fmt

stock ReplaceString(const str[], const regexp[], const fmt[], dest[], size = sizeof dest)
{
    new Regex:r = Regex_New(regexp);

    if (r)
    {
        Regex_Replace(str, r, fmt, dest, MATCH_DEFAULT, size);

        Regex_Delete(r);
    }
}

main()
{
    new str[128];

    ReplaceString("Pawn.        CMD",  " ", "", str);

    printf("%s", str);
} 

Error: [Pawn.Regex] Plugin::n_regex_replace: invalid str or fmt

Word boundary (\b)

This is my code:

public OnPlayerText(playerid, text[])
{
    new Regex:r = Regex_New("\b(word1|word2|word3)\b", REGEX_ICASE),
        RegexMatch:m,
        startpos, pos;

    if(Regex_Search(text, r, m, pos, startpos))
    {
        SendClientMessage(playerid, 0xF154ACFF, "Detected");
        return 0;
    }

    Regex_Delete(r);
    return 1;
}

This is how it should work:

https://i.imgur.com/wtbb9Wi.png

When I type word1 or word2 or word3 ingame, nothing happens. But, when I remove '\b' from regex pattern it works, but not the way I want. Without using '\b' it will capture word even if it's mixed within another word.
Like this:

https://i.imgur.com/1LdPxtl.png

Version issue with windows plugin

#define PAWNREGEX_INCLUDE_VERSION 112

It would not work with windows plugin version as 112 but it would work as #define PAWNREGEX_INCLUDE_VERSION 111
I have tried building the latest files in both windows and linux but no effect in windows while its working with linux.

And this goes opposite for linux. If its 112 then its fine and if its 111 then it dosent.

Ignores SA:MP naming conventions

Most of SA:MP (core, libraries, and plugins) are written as:

Library_FunctionName, but this for some reason ignores that and uses library_function_name. Can this be corrected to better match all other code?

error 027: invalid character constant

Hello!,
how can I evade this issue? It is a correct regex.

error 027: invalid character constant

Line: ReplaceString(info, "\{[0-9A-F]{6}\}", " ", infoa);

Best regards and thanks for your time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.