Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

Quick regex help (split between 2 chars)

This topic is 4500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to figure the regex for splitting a string based on sequences in between two characters: '=' and '&'. So the string "=abc&xxx=123&" would give me "abc", "123".

Share this post


Link to post
Share on other sites
Advertisement
ViLiO: Nope, thanks though.

I tried: /[^&]+[=$]/, and that works except it gives me an extra '&' at the end of every string.

Share this post


Link to post
Share on other sites
What language are you using? Normally splitting functionality will be provided separately from matching and capturing.

Share this post


Link to post
Share on other sites
Regex regex = new Regex("=(?<match>[^&]+)&");

MatchCollection matches = regex.Matches(inputString);

foreach (Match match in matches)
{
match.Groups["match"].Value; // This is each result between a = and &
}

Share this post


Link to post
Share on other sites
In PHP, I'd use the following approach.


$arr = preg_match("/=|\&",$inputstr);
for($i = 0; $i < len($arr); $i += 2)
{
$matches[$arr] = $arr[i+1];
}


Should be easily adapted to another language. For instance, in C#:


using System.Text.RegularExpressions;
using System.Collections.Specialized;

StringDictionary matches = new StringDictionary ();
string[] arr = Regex.Split( inputString, "=|\&" ) );
for( int i = 0; i < arr.Count; i += 2 )
matches.Add( arr[ i ], arr[ i + 1 ] );

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!