locked
Use Regex.Replace to get the same value back RRS feed

  • Question

  • string output = Regex.Replace(input, pattern, replacement);

    I get replacement patterns from a database to transform inputs into an expected format.

    I need a replacement pattern that will return the same value as the input.

    I tried (.) but that did not work.

    What do I need?

    Thanks,


    Jon Jacobs
    In transmission, subatomic particles managed by professionals.
    No innocent electrons were harmed.

    Thursday, July 17, 2014 1:08 AM

Answers

  • Eureka! It was so simple and obvious!


                //represents a piece of data captured from a particular script log file
                string input = @"\\server01\folder2\folder24\folder241\somefile.pdf";

                //would be read from a table entry for that log type
                string pattern = "DoNotMatch";

                //would be read from a table entry for that log type
                string replacement = "DoesNotMatter";

                //executed for every entry for all logs.
                string output = Regex.Replace(input, pattern, replacement);

    If there is no match from the pattern, then the output will be the same as the input.

    Thanks to all who contributed.


    Jon Jacobs
    In transmission, subatomic particles managed by professionals.
    No innocent electrons were harmed.

    • Marked as answer by Jon Q Jacobs Thursday, July 17, 2014 1:20 PM
    Thursday, July 17, 2014 1:18 PM

All replies

  • How about this?

    int output = Int.Parse(Regex.Replace(input, pattern, replacement));


    jdweng

    Thursday, July 17, 2014 3:19 AM
  • Thanks. Rarely does the value I interpret (from various scripts in an ECM system) need to be a number. From some scripts I might get "signed out" and the tables associated with that script will need "Y" stored in the signed out column (or "N" if absent). From another script I might get "blah blah Copied to <some url>" and one regex I read from the associated tables extracts the url. The replacement regex I also read from those tables needs to transform the url to... itself, which will be stored in the CopiedTo column of one of the tables.

    In other words, I have a real-time application that parses the logging information from numerous scripts and extracts relevant information, storing it in searchable tables. The format of information from the scripts varies widely and my database tables contain data capture and data transforing regular expressions suitable for each script log type, including the names of specialized auxiliary tables along with the template for the insert statement.

    That is why, for some scripts, I need a regular expression that would transform a captured value into itself.

    Thanks,


    Jon Jacobs
    In transmission, subatomic particles managed by professionals.
    No innocent electrons were harmed.


    Thursday, July 17, 2014 11:34 AM
  • I have exracted lot of tagged data using Regex in the past.  I usually like to use XMLDocuments to get the tags, but have used both .  Without seeing some examples of the data and your code it is hard to give sample code.  There are a few diffferent styles of XML and you may have HTML instead of XML.  I'm also not sure if you are trying to get attributes, or just tags and values. 

    jdweng

    Thursday, July 17, 2014 11:40 AM
  • Thank you. I am reading logging data from numerous scripts with varying formats all in flat text files. Rather than hard-coding the rules for each different script log, my application reads the regular expressions needed for the partular script from database tables. For most scripts I transform the extracted values into something suitable for the output tables to be searched on. In a few cases, the value needs to be unchaged before storage.

    That is why I need a replacement expression that will give output identical to the input.

    Thanks,


    Jon Jacobs
    In transmission, subatomic particles managed by professionals.
    No innocent electrons were harmed.

    Thursday, July 17, 2014 11:53 AM
  • You regex expression should have or'd expressions using '|'.  The expression is searched from left to right.  So if you put as the last or'd expression in the right a wild card '.*' it will match everything that wasn't matched in other parts or'd parts of the expression.

    jdweng

    Thursday, July 17, 2014 11:56 AM
  • Eureka! It was so simple and obvious!


                //represents a piece of data captured from a particular script log file
                string input = @"\\server01\folder2\folder24\folder241\somefile.pdf";

                //would be read from a table entry for that log type
                string pattern = "DoNotMatch";

                //would be read from a table entry for that log type
                string replacement = "DoesNotMatter";

                //executed for every entry for all logs.
                string output = Regex.Replace(input, pattern, replacement);

    If there is no match from the pattern, then the output will be the same as the input.

    Thanks to all who contributed.


    Jon Jacobs
    In transmission, subatomic particles managed by professionals.
    No innocent electrons were harmed.

    • Marked as answer by Jon Q Jacobs Thursday, July 17, 2014 1:20 PM
    Thursday, July 17, 2014 1:18 PM