0

I have a string variable like:

string data= "#FirstName=Arvind #LastName= Chaudhary_009"

Using Regex in C# i Want the output like :

FirstName = Arvind;
LastName= Chaudhary009;
3
  • 1
    What have u tried so far? Commented Nov 15, 2016 at 2:06
  • is there some reason you have to use regex here? String.Split(new char[] {' ', '#'}, StringSplitOptions Remove Empty) and then string.Replace("=", " = ") for the specific string example you give. Is there some variability in the input that makes it harder? Commented Nov 15, 2016 at 2:17
  • yes i want to do it in only c# because of some security purpose if you can do it than please Commented Nov 15, 2016 at 2:20

2 Answers 2

1

There would be more ways of doing this. Two of them would be

string data = "#FirstName=Arvind #LastName= Chaudhary_009";
data = data.Replace("_", "");
data = data.Replace("=", " = ");
string[] dt = data.Split(new char[] {'#'}, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(dt[0]); // Print your array here

Regex: Which you asked for

Regex regex = new Regex(@"#");
string[] dt1 = regex.Split(data).Where(s => s != String.Empty).ToArray();
Console.WriteLine(dt1[0]); // Print your array here

You can print array the way you want

Edit

After Understanding the requirements from comments

string data = "#FirstName=Arvind #LastName= Chaudhary_009";
data = data.Replace("_", "");
string[] dt = data.Split(new char[] {'#'}, StringSplitOptions.RemoveEmptyEntries);

Regex regex = new Regex(@"#");
string[] dt1 = regex.Split(data).Where(s => s != String.Empty).ToArray();

foreach(string d in dt)
{
    //this will print both the line
    Console.WriteLine(d);
}

foreach(string d in dt1)
{
    //this will print both the line
    Console.WriteLine(d);
}

Screenshot for showing the output

Sign up to request clarification or add additional context in comments.

7 Comments

I don't know the exact requirements here but, there are three things missing from your solution. Removing the _ from the last name, changing the second # into a ; and adding a ; at the end
Yeah the requirements are not clear. Whereas I assumed OP used ; for telling that two splits of the string. and Yeah I completely ignored _ in my solution. Lemme fix that.
string data = "#FirstName=Arvind #LastName= Chaudhary_009"; and want FirstName=Arvind; and LastName= Chaudhary009
@Arvindchaudhary: does the code serve you what you were looking for?
If no space between the = than just ignore that line.
|
0

There will be many solutions. I suggest you use .NET Regex Tester or a similar online tool to help develop a regex that works well.

A simple example regex that will give you some groups:

#FirstName\s*=\s*(.*)\s?#LastName\s*=\s*(.*)_(.*)

Run that and then format up the output based on groups 1, 2, 3.

Comments

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.