locked
regex to match a partial string RRS feed

  • Question

  • i want  a regex to match a string even if there is one character missing in the original string.

    For eg. i want  a regex to take  "abcd" as a match and  @bcd,a@bcd,,ab@d,abc@ is found where @ can be any character but there can be only one such deviation at max.

    Monday, June 28, 2010 5:49 AM

Answers

  • (?x)
    (?:a|(?<g>.))
    (?:b|(?<g>(?(g)(?!)|.)))
    (?:c|(?<g>(?(g)(?!)|.)))
    (?:d|(?<g>(?(g)(?!)|.)))


    Regards
    • Proposed as answer by Kirill Polishchuk Monday, June 28, 2010 7:36 AM
    • Marked as answer by SamAgain Tuesday, July 6, 2010 7:16 AM
    Monday, June 28, 2010 7:35 AM

All replies

  • (?x)
    (?:
    .bcd
    |
    a.cd
    |
    ab.d
    |
    abc.
    )


    Regards
    Monday, June 28, 2010 6:22 AM
  • thanx for ur reply...

    but my problem is that strings are quite long..so i will have to explicitly mention each combination using pipeline...do we have a regex that could make any one and  only one of the character optional.

     

    Regards

    Monday, June 28, 2010 6:42 AM
  • (?x)
    (?:a|(?<g>.))
    (?:b|(?<g>(?(g)(?!)|.)))
    (?:c|(?<g>(?(g)(?!)|.)))
    (?:d|(?<g>(?(g)(?!)|.)))


    Regards
    • Proposed as answer by Kirill Polishchuk Monday, June 28, 2010 7:36 AM
    • Marked as answer by SamAgain Tuesday, July 6, 2010 7:16 AM
    Monday, June 28, 2010 7:35 AM