Trying to prevent duplicates in preg_replace Regex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • erikcw

    #1

    Trying to prevent duplicates in preg_replace Regex

    Hi all,

    I'm trying to write a regex pattern to use in preg_replace. Basically
    I want to put [brackets] around every line (\n) in this variable.
    However, I need to exclude lines that already have brackets or
    quotation marks around them as well as lines that start with a
    hyphen. Lastly the lines with ** need the brackets before the **,
    instead of at the end of the line.

    Here is what I have so far - it is close, but I keep ending up with
    duplicate brackets.

    $temp = 'this
    is
    a
    test
    "this"
    "is"
    "a"
    [this]
    [is]
    [a]
    test ** 93.23
    test 2 ** 10.05
    -this
    -is
    -a
    another good test
    ';
    $PATTERNS = array("/^[^\"\[]/m", "/[^\"\]]$/m", "/[\r\n]+/", "/ \*\*
    (.*)]/");
    $REPLACEMENTS = array("[", "]", "]\n[", "] \*\* $1");
    $output1 = trim(preg_repla ce($PATTERNS, $REPLACEMENTS, trim($temp)));
    print_r($output 1);

    Thanks for your help!
    Erik

  • Toby A Inkster

    #2
    Re: Trying to prevent duplicates in preg_replace Regex

    erikcw wrote:
    I'm trying to write a regex pattern to use in preg_replace. Basically
    I want to put [brackets] around every line (\n) in this variable.
    However, I need to exclude lines that already have brackets or
    quotation marks around them as well as lines that start with a
    hyphen. Lastly the lines with ** need the brackets before the **,
    instead of at the end of the line.
    $x ='...'; // orig string

    $x = preg_replace('/^(.*)$/m', '[$1]', $x);
    $x = preg_replace('/^\[\"(.*)\"\]$/m', '"$1"', $x);
    $x = preg_replace('/^\[\[(.*)\]\]$/m', '[$1]', $x);
    $x = preg_replace('/^\[(.*)(\s*\*\*.*) \]$/m', '[$1]$2', $x);
    $x = preg_replace('/^\[\]/m', '', $x);

    That last regexp covers the behaviour of empty lines, and lines that start
    with a double-asterisk.

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
    Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

    * = I'm getting there!

    Comment

    • Janwillem Borleffs

      #3
      Re: Trying to prevent duplicates in preg_replace Regex

      erikcw wrote:
      Here is what I have so far - it is close, but I keep ending up with
      duplicate brackets.
      >
      [...]

      Often, simplicity = bless:

      $PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/');
      $REPLACEMENTS = array('', '[$1]');


      JW


      Comment

      • erikcw

        #4
        Re: Trying to prevent duplicates in preg_replace Regex

        On Feb 24, 9:45 am, "Janwillem Borleffs" <j...@jwscripts .comwrote:
        erikcw wrote:
        Here is what I have so far - it is close, but I keep ending up with
        duplicate brackets.
        >
        [...]
        >
        Often, simplicity = bless:
        >
        $PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/');
        $REPLACEMENTS = array('', '[$1]');
        >
        JW
        Thank you for your reply.

        Your selution is simple and elegant. Unfortunatly, it puts brackets
        around every line. I need it to ignore lines with "" around them, as
        well as line that start with a '-' and lines that end in a ** 0.00
        need the ending bracket before the **.

        So, the output should look like this:
        $input = 'this
        is
        a
        test
        "this"
        "is"
        "a"
        [this]
        [is]
        [a]
        test ** 93.23
        test 2 ** 10.05
        -this
        -is
        -a
        another good test
        ';

        $OUTPUT = '[this]
        [is]
        [a]
        [test]
        "this"
        "is"
        "a"
        [this]
        [is]
        [a]
        [test] ** 93.23
        [test 2] ** 10.05
        -this
        -is
        -a
        [another good test]
        ';

        What needs to be changed in your pattern to do this?

        Thanks so much!
        Erik

        Comment

        • Rik

          #5
          Re: Trying to prevent duplicates in preg_replace Regex

          erikcw <erikwickstrom@ gmail.comwrote:
          Your selution is simple and elegant. Unfortunatly, it puts brackets
          around every line. I need it to ignore lines with "" around them, as
          well as line that start with a '-' and lines that end in a ** 0.00
          need the ending bracket before the **.
          Well, it can be done, but it will be quite cumbersome/clumsy. Let's
          backtrack: why do you need the data like that?
          --
          Rik Wasmus

          Comment

          • erikcw

            #6
            Re: Trying to prevent duplicates in preg_replace Regex

            On Feb 24, 1:31 pm, Rik <luiheidsgoe... @hotmail.comwro te:
            erikcw <erikwickst...@ gmail.comwrote:
            Your selution is simple and elegant. Unfortunatly, it puts brackets
            around every line. I need it to ignore lines with "" around them, as
            well as line that start with a '-' and lines that end in a ** 0.00
            need the ending bracket before the **.
            >
            Well, it can be done, but it will be quite cumbersome/clumsy. Let's
            backtrack: why do you need the data like that?
            --
            Rik Wasmus
            I need to format the data this way for a legacy system.

            Comment

            • erikcw

              #7
              Re: Trying to prevent duplicates in preg_replace Regex

              On Feb 24, 1:54 pm, "erikcw" <erikwickst...@ gmail.comwrote:
              On Feb 24, 1:31 pm, Rik <luiheidsgoe... @hotmail.comwro te:
              >
              erikcw <erikwickst...@ gmail.comwrote:
              Your selution is simple and elegant. Unfortunatly, it puts brackets
              around every line. I need it to ignore lines with "" around them, as
              well as line that start with a '-' and lines that end in a ** 0.00
              need the ending bracket before the **.
              >
              Well, it can be done, but it will be quite cumbersome/clumsy. Let's
              backtrack: why do you need the data like that?
              --
              Rik Wasmus
              >
              I need to format the data this way for a legacy system.
              What about this:
              $PATTERNS = array('/([^\r\n]+)/', '/\[\[(.*)]]/', '/\["(.*)"]/', '/\[-
              (.*)]/', '/ \*\* (.*)]/');
              $REPLACEMENTS = array('[$1]', '[$1]', '"$1"', '-$1', '] ** $1');

              I guess the question is, is it faster to run several small patterns or
              to run a single larger pattern that handles all possible cases?

              Thanks for your help!
              Erik

              Comment

              • Janwillem Borleffs

                #8
                Re: Trying to prevent duplicates in preg_replace Regex

                erikcw wrote:
                Your selution is simple and elegant. Unfortunatly, it puts brackets
                around every line. I need it to ignore lines with "" around them, as
                well as line that start with a '-' and lines that end in a ** 0.00
                need the ending bracket before the **.
                >
                Perhaps with some fix up patterns:

                $PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/', '/(\["|"\])/',
                '/(\[)\-(.+)(\])/',
                '/(\[)(.+)([*]+)(.+)(\])/');
                $REPLACEMENTS = array('', '[$1]', '"', '-$2', '[$2$3]$4');


                JW


                Comment

                • Janwillem Borleffs

                  #9
                  Re: Trying to prevent duplicates in preg_replace Regex

                  Janwillem Borleffs wrote:
                  $PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/', '/(\["|"\])/',
                  '/(\[)\-(.+)(\])/',
                  '/(\[)(.+)([*]+)(.+)(\])/');
                  $REPLACEMENTS = array('', '[$1]', '"', '-$2', '[$2$3]$4');
                  >
                  Err:

                  $PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/', '/(\["|"\])/',
                  '/(\[)\-(.+)(\])/',
                  '/(\[)(.+)([*]{2})(.+)(\])/');
                  $REPLACEMENTS = array('', '[$1]', '"', '-$2', '[$2]$3$4');


                  JW


                  Comment

                  Working...