#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Terlingua, TX
    Posts
    15
    Rep Power
    0

    [.NET] New to Regex


    I've been using some simple regex in some Visual Basic 2005 code and all has been working well. What I fond now is that I need to do a bit more complex replace.
    Given a string that begins with "(jr012) ...."
    I want to replace the (jrxxx) with "Jack Russell xxx -"
    I've been reading and every time I think I know what to do the replace just gives me back the original string.
    Suggestions - please!
    //al
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,296
    Rep Power
    7170
    If it's giving you back the original string, that probably means your pattern isn't matching. What pattern are you trying?
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Terlingua, TX
    Posts
    15
    Rep Power
    0
    Originally Posted by E-Oreo
    If it's giving you back the original string, that probably means your pattern isn't matching. What pattern are you trying?
    well, I kinda figured that ...

    \(jr([0-9]*)\) as the regex
    and
    (Jack Russell \1) as the replacement ...
    with the passed string looking like
    "(jr155) Some book title"

    of the two regex "helper" utilities is have, one says that it matches nothing (regex from Notepad++ plug in) and the other (Expresso) tells me that it's not a proper regex
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,296
    Rep Power
    7170
    Hm, well the regex looks correct to me... Maybe post your .NET code and someone who knows that will be able to spot some language-specific issue.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Terlingua, TX
    Posts
    15
    Rep Power
    0
    Originally Posted by E-Oreo
    Hm, well the regex looks correct to me... Maybe post your .NET code and someone who knows that will be able to spot some language-specific issue.
    I'm reading in a file that contains both "normal" substitutions and regex substitutions but the operative part looks like
    Code:
            For Each kvp As KeyValuePair(Of String, strReplaceType) In ReplaceDict
                If kvp.Value.ReplaceType = "Regex" Then
                    Dim rg As New Regex(kvp.Key, RegexOptions.IgnoreCase)
                    strfile = rg.Replace(strfile, kvp.Value.WithThis)
                Else
                    loc = strfile.IndexOf(kvp.Key)
                    If loc >= 0 Then strfile = Strings.Replace(strfile, kvp.Key, kvp.Value.WithThis, 1, -1, CompareMethod.Text)
                End If
            Next
    Now, I'll admit that I don't really know what I'm doing, but I would think that when it find the kvp.key (the regular expression) it should substitute the kvp.value but as I said, I'm getting back what I gave it.
    BTW, thanks Oreo ...
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Terlingua, TX
    Posts
    15
    Rep Power
    0
    Found the error.
    The line I typed above as:
    (Jack Russell \1) as the replacement ...
    was actually
    \(Jack Russell \1\) as the replacement ...
    Where I was erroneously escaping the parenthesis in the text in the output from the replace. That was giving me a file error - and I already trap file errors for duplicate files.
    The replacement text was coming out as c:\output\\(Jack Russell 155) ... and for some reason (tongue in cheek) windows was objecting to a file name with a double back slash in it.
    Your comment that it looked right made me look closer at what was happening in my code!
    What's a good regex tool ... something I can give regex / text and have it do the substitution in test mode ... having to recompile every time I want to check out the results is a pain!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0
    you could use preg_replace function in php
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Or Perl in debug mode.

    Just a very simple example:

    Code:
    $ perl -de 42
    
    Loading DB routines from perl5db.pl version 1.32
    Editor support available.
    
    Enter h or `h h' for help, or `man perldebug' for more help.
    
    main::(-e:1):   42
      DB<1> $c = "foobar"
    
      DB<2> print "true" if $c =~ /barf/
    
      DB<3> print "true" if $c =~ /foob/
    true
      DB<4>

IMN logo majestic logo threadwatch logo seochat tools logo