[RESOLVED] Regex - [key={0}]-VBForums
Results 1 to 13 of 13

Thread: [RESOLVED] Regex - [key={0}]

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Resolved [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)

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,833

    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



    www.techreceipe.tk


  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Regex - [key={0}]

    I see a few patterns for this based on how you want the result:
    1. 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:
      Code:
      images/logo.png
    2. 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:
      Code:
      images/logo.png
    3. 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'

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Regex - [key={0}]

    great. thanks all

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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 03:09 PM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Regex - [key={0}]

    Just to clarify, your string that you are trying to match can be inbetween a string with any content?

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Regex - [key={0}]

    correct

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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]. "

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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 03:45 PM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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)
                {
                 }

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  12. #12
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Regex - [key={0}]

    Is this resolved?

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Regex - [key={0}]

    yup.. will mark it as that

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.