Caresi
Caresi

Reputation: 69

Regex replace tags in C#

I'm making a small parser in C#.

My string to parse is:

Hello [Name] [LastName] how are you? [FirstTime]this is your first time isnt it?[/FirstTime] Bye!

My C# code for the [Name] and [LastName] is:

  message = message
    .Replace("[FirstName]", user.FirstName)
    .Replace("[LastName]", user.LastName)
bool isFirstTime = user.FirstLogin;

My problem is between [FirstTime] [/FirstTime]. If isFirstTime is true I want to keep the string between the tags and if it's false I want to throw away the text inside the tags.

Any idea how I could go for this problem?

Example result:

Hello James Bond how are you? this is your first time isnt it? Bye!

EDIT: I forgot to say that the example string is just well an example and the string is "user defined" so using a stringbuilder isn't gonna work in my case.

Upvotes: 2

Views: 1999

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627488

Here is a regex-way you might be looking for:

var FirstLogin = false;
var FirstName = "James";
var LastName = "Bond";
var message = "Hello [FirstName] [LastName] how are you? [FirstTime]this is your first time isnt it?[/FirstTime] Bye!";
message = message
         .Replace("[FirstName]", FirstName)
         .Replace("[LastName]", LastName);
var isFirstTime = FirstLogin;
if (FirstLogin)
    message = Regex.Replace(message, @"\[/?FirstTime\]", string.Empty);
    // Hello James Bond how are you? this is your first time isnt it? Bye!
else
    message = Regex.Replace(message, @"\[FirstTime\].*?\[/FirstTime\]", string.Empty);
    // Hello James Bond how are you?  Bye!

REGEX EXPLANATION:

  1. \[/?FirstTime\] - Matches [FirstTime] or [/FirstTime] literally
  2. \[FirstTime\].*?\[/FirstTime\] - Matches any text between the closest occurrences of [FirstTime] and [/FirstTime] (not accounting for nested BBtags).

If you have nested tags, you might consider 2 regexes:

\[FirstTime\](?:(?!\[/?FirstTime\]).)*\[/FirstTime\]

This one above will match the closest pair of FirstTime tag. And the one below can match those farthest:

\[FirstTime\](?:\[([^]]*)\].*?\[/\1\]|.*?)*\[/FirstTime\]

Upvotes: 2

Revils
Revils

Reputation: 1508

You can use the stringbuilder class:

StringBuilder builder = new StringBuilder();
builder.Append("Hello ");

//Name
builder.Append(Name);
builder.Append(" ");
builder.Append(LastName);
builder.Append(" How are you? ");

//First time
if(FirstTime){
    builder.Append(" This is your first time isn't it? ");
}

//Bye
builder.Append(" Bye!");

//Convert to string
String sentence = builder.ToString();

Upvotes: 0

Ali Adlavaran
Ali Adlavaran

Reputation: 3735

use this regex pattern to find and replace FirstName block:

[[FirstTime]](.*)[[/FirstTime]]

then your code may be :

Regex rgx = new Regex("[[FirstTime]](.*)[[/FirstTime]]");
File.WriteAllText("", rgx.Replace("Hello [Name] [LastName] how are you? [FirstTime]this is your first time isnt it?[/FirstTime] Bye!", "Ali"));

Upvotes: 1

Related Questions