Hi
im not great with regex. I need a regex to obtain everything after '=' in this:
[AttachImage=images/logo.png]
so everything after [AttachImage=
so I can obtain "images/logo.png"
what would the regex be? (strictly regex only please)
Printable View
Hi
im not great with regex. I need a regex to obtain everything after '=' in this:
[AttachImage=images/logo.png]
so everything after [AttachImage=
so I can obtain "images/logo.png"
what would the regex be? (strictly regex only please)
This will capture the images/logo.png]
and remove the last ] manuallyCode:Dim str As String = "[AttachImage=images/logo.png]"
Dim reg As New Regex("[^=]+$")
If reg.IsMatch(str) Then
Dim capturevalue As String = reg.Match(str).Value
End If
I see a few patterns for this based on how you want the result:
- The key has to be 'AttachImage':
Output:Code:string text = "[AttachImage=images/logo.png]";
Regex regex = new Regex(@"^\[AttachImage\=(?<m>[^=\]]+)\]$");
var match = regex.Match(text);
if (match.Success)
Console.WriteLine(match.Groups["m"].Value);
Code:images/logo.png
- You just want to match any text after a "=" and before a "]":
Output:Code:string text = "[AttachImage=images/logo.png]";
Regex regex = new Regex(@"(?<=\=)[^\]]+");
var match = regex.Match(text);
if (match.Success)
Console.WriteLine(match.Value);
Code:images/logo.png
- You want both the key and value returned:
Output:Code:string text = "[AttachImage=images/logo.png]";
Regex regex = new Regex(@"^\[(?<k>[^\=]+)\=(?<v>[^\]]+)\]$");
var match = regex.Match(text);
if (match.Success)
{
Console.WriteLine("Key='{0}'", match.Groups["k"].Value);
Console.WriteLine("Value='{0}'", match.Groups["v"].Value);
}
Code:Key='AttachImage'
Value='images/logo.png'
great. thanks all
ok trying to implement it and not quite working.
im reading a txt file which contains for example.. an email template.
there are certain "reserved" areas where that area of text will be replaced with a value.
however the above does not appear to work. I have also added another thing I want to match.
This fails.Code:
string fileRead = "dear [title]. Blah blah [activation]. [AttachImage=images/companyLogo.jpg&cid:companyLogo]";
// perform replacements of [title] and [activation]
// now replace the attach image
Regex attachImgRegex = new Regex(@"^\[(?<k>[^\=]+)\=(?<v>[^\]]+)\&(?<cid>[^\:]]+)\]$");
var match = attachImgRegex.Match(fileRead);
if (match.Success)
{
...
}
I do need the entire string returned ([AttachImage=images/companyLogo.jpg&cid:companyLogo]) in addition to each section (images/companyLogo.jpg, cid:companyLogo)
Just to clarify, your string that you are trying to match can be inbetween a string with any content?
correct :)
Remove the anchoring characters from the pattern, these are the ^ character and $ character at the beginning and end, respectively and try that.
Thanks! Almost there.
The problem now is that it does not exactly find that string pattern. it finds the others (which I dont need!) like:
[Title]
then when doing a match.Groups["k"].Value I get:
title]. Blah blah [activation]. "
let me make it clear (sorry!) :)
lets use this:
\[AttachImage\=(?<m>[^=\]]+)\]
I only want to match the key "AttachImage".
I want a slight tweak now where I want to match this instead:
[AttachImage=somePath/logo.jpg&cid:Test]
so it returns me: somePath/logo.jpg AND also: cid:Test
where the key IS AttachImage
I may have several of these in a single string... so want to be sure I can iterate through each one and replace with what I need to replace the original string with
got it...
Code:System.Text.RegularExpressions.MatchCollection attachImgRegex = System.Text.RegularExpressions.Regex.Matches(emailTemplate, @"\[AttachImage\=(?<m>[^=\]]+)\&cid:(?<o>[^=\]]+)\]");
foreach (System.Text.RegularExpressions.Match currentMatch in attachImgRegex)
{
}
Is this resolved? :)
:) yup.. will mark it as that ;)