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

    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Ask for a regular expression


    Hi all,

    Could you give me the regex for the following requirement:
    1. abc{0bcd -> abc{0}bcd
    2. cde0}abc -> cde{0}abc


    For example:

    1. Sample1:{ 0 is sucker than the 1 } } sample.
      ->
      Sample1:{0} is sucker than the {1} sample.
    2. 0} Ab {1}Cde { 2 } } f3{4 5ij { {{6kl{ 7m8}}nop9 10 } qrst11 }12Uvt{ xyz}13{{14
      ->
      {0} Ab {1}Cde {2} f3{4} 5ij {6}kl{7}m{8}nop9 {10} qrst{11}12Uvw{ xyz}13{14}


    Note:

    1. { or }: can be multiple with spaces, for example, "{","{ ", "}}", "{ {{ "
    2. 0: can be multi-digit, for example: 0, 12, 123 etc.
    3. abc: any string, can be null
    4. bcd: any string, which can be null and include white space, but doesn't start with 0-9
    5. cde: any string, which can be null and include white space , but doesn't end with 0-9
    6. ->: means "Change into"


    Thanks,
    Andrew Huang
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    29
    Rep Power
    0
    Try

    Code:
    Search for: (?:{ *)*(\d+)(?: *})+|(?:{ *)+(\d+)(?: *})*
    Replace with: {\1\2}
  4. #3
  5. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Nice one, aba!

    No issue whatsoever with your regex, love it. But another (twisted) idea building on yours---just for fun:
    Code:
    ({ *)*(\d+)( *})*(?(1)|(?(3)|(?!)))
  6. #4
  7. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93

    Advanced Regex Tut: Conditionals and Failure


    I forgot to provide the replacement expression:
    {\2}

    A bit of explanation about the expression in the previous post. It deserves a few words because it is a bit unusual. So this will be a bit of an...

    Advanced Regex Tut: Conditionals and Failure

    In the original expression, abareplace had to design the regex to ensure that either the left curly brace or the right curly brace is set (at least one of the two) so we don't perform the replacement on unrelated numbers. To do so, aba used an alternation. IMO that was a great choice as that solution will be supported in a large number of contexts (various regex engines).

    In PCRE (used in PHP among others), we have conditionals. So instead of using an alternation to enforce that at least the left or the right brace is used, we perform a single match where both braces are optional. The braces are also captured. For the first one: ({ *)
    Therefore, the first brace is Group 1, the digit(s) is Group 2, the closing brace is Group 3.

    After the match, we say:
    If Group 1 is set... [NOTHING]
    OR
    If Group 3 is set... [NOTHING]
    OR
    FAIL

    That way, if neither brace is used, the expression fails.
    "If Group 1 is set" is expressed by (?(1)
    "Fail" is expressed by (?!)

    Not that it matters, but this expression should usually match or fail a bit faster as the regex engine has less matching to do.

    It can be sped up a bit more by introducing some possessive quantifiers:
    Code:
    ({ *+)*+(\d+)( *+})*+(?(1)|(?(3)|(?!)))
    Wishing you all a fun weekend!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    29
    Rep Power
    0
    Ragax, thank you very much for the explanation. It's a fantastic example of conditionals.
  10. #6
  11. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Hi abareplace, I thought you'd like it (and thank you for your explanations on other posts!) Yes, I was excited to see that working too... it doesn't come up that often that you can use conditionals in a way that saves the engine a bit of work.
    Wishing you a fun weekend.

IMN logo majestic logo threadwatch logo seochat tools logo