View Single Post
Old 18/02/2019, 07:25 AM   #32
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,055
Reputation: 172
Default Re: Pawn.Regex - full support of regular expressions

Quote:
Originally Posted by [DK]Dark_Knight View Post
Plugin works great

However how does one replace text with nothing
Code:
new Regex:r = Regex_New("Test ");
Regex_Replace("My Test Text", r, "", str);
Without getting the error
Code:
[Pawn.Regex] Plugin::n_regex_replace: invalid str or fmt
If i put any text into the format it works sweet as, but with nothing error.
Normally I would open up the plugin source and sus it out myself, however I am out of town for a while and only have my craptop
First method: group begin-end and concatenate them.

pawn Code:
main()
{
    new Regex: r = Regex_New("^(.+?)?Test\\s(.+?)?$");
   
    if (r)
    {
        new RegexMatch: m,
            group_1[128], group_2[128],
            group_1_length, group_2_length;

        if (Regex_Match("My Test Text", r, m))
        {
            Match_GetGroup(m, 1, group_1, group_1_length);
            Match_GetGroup(m, 2, group_2, group_2_length);

            Match_Free(m);
           
            strcat(group_1, group_2);
            print(group_1);
        }
       
        Regex_Delete(r);
    }
}

Second method: find and replace special character.

pawn Code:
main()
{
    new Regex:r = Regex_New("Test ");
   
    if (r)
    {
        new str[128];
       
        Regex_Replace("My Test Text", r, "\1", str);
        Regex_Delete(r);
       
        strreplace(str, "\1", "");
        print(str);
    }
}

// `strreplace` function from Slice's strlib

Last edited by Calisthenics; 18/02/2019 at 08:21 AM.
Calisthenics is offline   Reply With Quote