AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Regex - Help with loop (https://forums.alliedmods.net/showthread.php?t=242630)

... 06-22-2014 08:45

Regex - Help with loop
 
In case i have a string
PHP Code:

new string[] = "str1-str2-str3-str4" 

I am trying to get those data between'em '-'
I tried the following regex pattern , guess what no success
PHP Code:

(\w+)- 

Second pattern i tried was also a fail.
PHP Code:

((\w+)-)+ 

Can someone write down how to make a regex pattern which will loop throughout the string and get all those data in between '-' , Note - my string can contain any amount of '-' , i already did it using strtok , but now i want it to be done in regex.
I had try using 's' flag , no success . All i get is "str1" as the output

Black Rose 06-22-2014 10:24

Re: Regex - Help with loop
 
How are you reading the output? Do the words between the dashes contain anything else than letters and numbers?

Backstabnoob 06-22-2014 10:32

Re: Regex - Help with loop
 
You might also be interested in the str_explode function from this inc: https://forums.alliedmods.net/showthread.php?t=163205

grs4 06-22-2014 10:33

Re: Regex - Help with loop
 
Use Explode function from php ;p
Code:


new string[] = "str1-str2-str3-str4" 
new str[4][5], num

explode(string, '-', str[num++], 3, 4)

client_print(0,3, "%s %s %s %s", str[0], str[1, str[2], str[3])
stock explode(const string[],const character,output[][],const maxs,const maxlen){
   
    new    iDo = 0,
    len = strlen(string),
    oLen = 0;
   
    do{
        oLen += (1+copyc(output[iDo++],maxlen,string[oLen],character))
    }while(oLen < len && iDo < maxs)
}


... 06-22-2014 10:36

Re: Regex - Help with loop
 
I just a need a solution for regex , please stick regex .

Black Rose 06-22-2014 10:59

Re: Regex - Help with loop
 
Quote:

Originally Posted by Black Rose (Post 2155617)
How are you reading the output? Do the words between the dashes contain anything else than letters and numbers?


... 06-22-2014 11:03

Re: Regex - Help with loop
 
Nope just a '-' , i tried every possibilities even including white spaces in my pattern and string . The output string is actually read out from a file . Is regex too limited ?

Just detect str and push them all to array , would be very helpful :D
PHP Code:

str-str2-str88-str11 


Black Rose 06-22-2014 11:11

Re: Regex - Help with loop
 
I might be blanking out here but I think this is the only way:
Code:
#include <amxmodx> #include <regex> public plugin_init() {     register_plugin("Test Plugin 9", "", "[ --{-@ ]");         new ret;     new Regex:hRegex = regex_compile("\w+", ret, "", 0);     new str[] = "str1-str2-str3-str4";     new match[10];     new pos;     while ( regex_match_c(str[pos], hRegex, ret) != 0 ) {         regex_substr(hRegex, 0, match, charsmax(match));         server_print("String: %s", match);         pos = strfind(str, match) + strlen(match);     }     }

... 06-22-2014 11:17

Re: Regex - Help with loop
 
Okay , but i expected regex to do it without loop or somethig , anyways thanks

fysiks 06-22-2014 17:03

Re: Regex - Help with loop
 
You should be able to do it with submatches but you still need a loop to loop through the submatches.

Code:

^([^-]*)-([^-]*)-([^-]*)-([^-]*)$
or

Code:

^(\w*)-(\w*)-(\w*)-(\w*)$
It's been a while since I've used Regex in AMX Mod X so I can't remember for sure if you can use submatches.


All times are GMT -4. The time now is 21:11.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.