Reputation: 15430
I need to parse the following format @[Alphanumeric1](Alphanumeric2:Alphanumeric3)
from a long string. Below is my string:
This is a long text
@[Alphanumeric1](Alphanumeric2:Alphanumeric3) again long text
@[Alphanumeric11](Alphanumeric22:Alphanumeric33) again long text
@[Alphanumeric111](Alphanumeric222:Alphanumeric333)
I need all the occurence of(@[Alphanumeric1](Alphanumeric2:Alphanumeric3)
) to be replaced by value which is coming after colon(:) i.e I want the output as
This is a long text
Alphanumeric3 again long text
Alphanumeric33 again long text
Alphanumeric333
Upvotes: 1
Views: 474
Reputation: 2701
This should do the trick:
class Program
{
static void Main(string[] args)
{
string data = @"This is a long text
@[Alphanumeric1](Alphanumeric2:Alphanumeric3) again long text
@[Alphanumeric11](Alphanumeric22:Alphanumeric33) again long text
@[Alphanumeric111](Alphanumeric222:Alphanumeric333)";
Debug.WriteLine(ReplaceData(data));
}
private static string ReplaceData(string data)
{
return Regex.Replace(data, @"@\[.+?\]\(.*?:(.*?)\)", match => match.Groups[1].ToString());
}
}
Upvotes: 0
Reputation: 38436
The following regex should by able to handle that input:
(@\[[a-zA-Z0-9]+\]\([a-zA-Z0-9]+:(?<match>[a-zA-Z0-9]+)\))
Used in C#, the following will find & replace all instances in a string input
:
string output = Regex.Replace(input, @"(@\[[a-zA-Z0-9]+\]\([a-zA-Z0-9]+:(?<match>[a-zA-Z0-9]+)\))", "${match}");
Regex Explained:
( # beginning of group to find+replace
@ # match the '@'
\[ # match the '['
[a-zA-Z0-9]+ # alpha-numeric match
\] # match the ']'
\( # match the '('
[a-zA-Z0-9]+ # alpha-numeric match
: # match the ':'
(?<match> # beginning of group to use for replacement
[a-zA-Z0-9]+ # alpha-numeric match
)
\) # match the ')'
)
Upvotes: 0
Reputation: 2698
@\[[\w\d]*\]\([\w\d]*:([\w\d]*)\)
That will match the three strings above, and grab the alphanumeric string after the :
in group 1. Play with the regex here.
Upvotes: 2