-
Feb 2nd, 2012, 07:24 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Regex - [key={0}]
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)
-
Feb 2nd, 2012, 08:35 AM
#2
Re: Regex - [key={0}]
This will capture the images/logo.png]
Code:
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
and remove the last ] manually
Please mark you thread resolved using the Thread Tools as shown
-
Feb 2nd, 2012, 12:37 PM
#3
Re: Regex - [key={0}]
I see a few patterns for this based on how you want the result:
- The key has to be 'AttachImage':
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);
Output:
- You just want to match any text after a "=" and before a "]":
Code:
string text = "[AttachImage=images/logo.png]";
Regex regex = new Regex(@"(?<=\=)[^\]]+");
var match = regex.Match(text);
if (match.Success)
Console.WriteLine(match.Value);
Output:
- You want both the key and value returned:
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);
}
Output:
Code:
Key='AttachImage'
Value='images/logo.png'
-
Feb 2nd, 2012, 02:24 PM
#4
Thread Starter
PowerPoster
-
Feb 2nd, 2012, 04:05 PM
#5
Thread Starter
PowerPoster
Re: Regex - [key={0}]
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.
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)
{
...
}
This fails.
I do need the entire string returned ([AttachImage=images/companyLogo.jpg&cid:companyLogo]) in addition to each section (images/companyLogo.jpg, cid:companyLogo)
Last edited by Techno; Feb 2nd, 2012 at 04:09 PM.
-
Feb 2nd, 2012, 04:15 PM
#6
Re: Regex - [key={0}]
Just to clarify, your string that you are trying to match can be inbetween a string with any content?
-
Feb 2nd, 2012, 04:17 PM
#7
Thread Starter
PowerPoster
Re: Regex - [key={0}]
correct
-
Feb 2nd, 2012, 04:29 PM
#8
Re: Regex - [key={0}]
Remove the anchoring characters from the pattern, these are the ^ character and $ character at the beginning and end, respectively and try that.
-
Feb 2nd, 2012, 04:32 PM
#9
Thread Starter
PowerPoster
Re: Regex - [key={0}]
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]. "
-
Feb 2nd, 2012, 04:38 PM
#10
Thread Starter
PowerPoster
Re: Regex - [key={0}]
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
Last edited by Techno; Feb 2nd, 2012 at 04:45 PM.
-
Feb 2nd, 2012, 05:00 PM
#11
Thread Starter
PowerPoster
Re: Regex - [key={0}]
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)
{
}
-
Feb 2nd, 2012, 06:04 PM
#12
Re: Regex - [key={0}]
Is this resolved?
-
Feb 2nd, 2012, 06:37 PM
#13
Thread Starter
PowerPoster
Re: Regex - [key={0}]
yup.. will mark it as that
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|