0

I have a string like:

personname=aanch&personaddress=Jammu & Kashmir&personmobile=9876543210

My desired output is:

personname=aanch
personaddress=Jammu & Kashmir
personmobile=9876543210

Is it possible to use a Regular Expression to split the string on & and get the result above?

13
  • Not directly, but you can split it and then make a second pass which merges the shorter ones with their neighbours Commented May 2, 2012 at 10:24
  • if its AB&CD&EFG then what should be the output ? Commented May 2, 2012 at 10:27
  • If the string is not of any fixed size, like ABC&DE&F&GH&I and I want the output string like ABC DE F GH&I............& here is an character not separator... Commented May 2, 2012 at 10:30
  • 1
    @aanch: The rules are still not clear, at least to me. Can you define exactly when a & should be split on (and when not)? Commented May 2, 2012 at 10:31
  • 2
    If this is a query string, then & cannot appear unescaped in there, as far as I know. Are you doing percent-decoding first and now try damage control? Commented May 2, 2012 at 13:58

3 Answers 3

2

With the re-edit of the question, I do see a solution that might actually work:

splitArray = Regex.Split(subjectString, 
    @"&    # Split on a &
    (?=    # but only if it's followed by...
     \w+   # a series of alphanumeric characters
     =     # and an equals sign
    )      # End of lookahead assertion", 
    RegexOptions.IgnorePatternWhitespace);
Sign up to request clarification or add additional context in comments.

Comments

0

EDIT - With the change of question

 var regex = new Regex(@"&(?<=[^&=]+=)")

Which the break down is

& - match &

(?<=[^&=]+=) - Match but don't include atleast one character that isn't & or = followed by =

Alternatively if you actually after the names and values you can do the following

var regex=new Regex(@"(?>^|&)(?<name>.*?)=(?<value>.*?)(?=$|&[^&]+?=)");
var text="personname=aanch&personaddress=Jammu & Kashmir&personmobile=9876543210";
foreach (Match match in regex.Matches(text)) {
    var name=match.Groups["name"].Value;
    var value=match.Groups["value"].Value;
    // Do something with values
}

2 Comments

the criteria is not clear as aanch said that & which is covered with spaces is considered as a character not a separator.
@HemantMetalia The comments in the question were not visible when I posted, I've now added an edit, but I agree the JK&L example does not seem to the OP criteria
-1

But why do you need reg-ex to split the string. You can use normal C# string split function to get the desired result

string text = "ABC&DEF&GHI&JK&L&MNO&P&Q"
var splitString = text.split(text,"&");

The splitted string will be stored as an array in splitString

1 Comment

have you gone through the op of the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.