Raised This Month: $ Target: $400
 0% 

Regex - Help with loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
...
Junior Member
Join Date: Jun 2014
Old 06-22-2014 , 08:45   Regex - Help with loop
Reply With Quote #1

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

Last edited by ...; 06-22-2014 at 08:50.
... is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 06-22-2014 , 10:24   Re: Regex - Help with loop
Reply With Quote #2

How are you reading the output? Do the words between the dashes contain anything else than letters and numbers?
__________________

Last edited by Black Rose; 06-22-2014 at 10:24.
Black Rose is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 06-22-2014 , 10:59   Re: Regex - Help with loop
Reply With Quote #3

Quote:
Originally Posted by Black Rose View Post
How are you reading the output? Do the words between the dashes contain anything else than letters and numbers?
__________________
Black Rose is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-22-2014 , 10:32   Re: Regex - Help with loop
Reply With Quote #4

You might also be interested in the str_explode function from this inc: https://forums.alliedmods.net/showthread.php?t=163205
Backstabnoob is offline
grs4
Senior Member
Join Date: Dec 2010
Location: Poland
Old 06-22-2014 , 10:33   Re: Regex - Help with loop
Reply With Quote #5

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)
}

Last edited by grs4; 06-22-2014 at 10:33.
grs4 is offline
...
Junior Member
Join Date: Jun 2014
Old 06-22-2014 , 10:36   Re: Regex - Help with loop
Reply With Quote #6

I just a need a solution for regex , please stick regex .
... is offline
...
Junior Member
Join Date: Jun 2014
Old 06-22-2014 , 11:03   Re: Regex - Help with loop
Reply With Quote #7

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
PHP Code:
str-str2-str88-str11 

Last edited by ...; 06-22-2014 at 11:04.
... is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 06-22-2014 , 11:11   Re: Regex - Help with loop
Reply With Quote #8

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);     }     }
__________________
Black Rose is offline
...
Junior Member
Join Date: Jun 2014
Old 06-22-2014 , 11:17   Re: Regex - Help with loop
Reply With Quote #9

Okay , but i expected regex to do it without loop or somethig , anyways thanks
... is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-22-2014 , 17:03   Re: Regex - Help with loop
Reply With Quote #10

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.
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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