1

Say I have string1 = "Helloworld33"

I want to be able to check a string2 so that it does not contain any three character part of string1.

string2 could be any set of characters of any length.

For example:

string1 = "Helloworld33"

string2 = "ello" (invalid)

          "3hi3" (valid)

          "H e l l o" (valid)

          "Hw3" (valid)

          "I_^&_rld_37482" (invalid)

How can this be done through regex in python? Or is there a way to do it without?

EDIT: Also, is there a way to do it so it is NOT case-sensitive?

Much thanks

Vigorou
  • 25
  • 5
  • 2
    Have you tried anything yet? This could be accomplished inefficiently using substring and `in`. http://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-method , http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python – Matthew Sep 09 '13 at 19:13

4 Answers4

3

You can use the difflib module:

import difflib
def solve(strs, match, n):
    s = difflib.SequenceMatcher(None, strs, match)
    return all(item.size < n for item in s.get_matching_blocks())
... 
>>> solve('Helloworld33', 'ello', 3)
False
>>> solve('Helloworld33', '3hi3', 3)
True
>>> solve('Helloworld33', 'H e l l o', 3)
True
>>> solve('Helloworld33', 'Hw3', 3)
True
>>> solve('Helloworld33', 'I_^&_rld_37482', 3)
False
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

You can create all triplets from string1 and then check if any of these triplets occur in string2:

string1 = "Helloworld33"
triplets = set([string1[i:i+3] for i in range(len(string1) - 2)])
result = not any(t in string2 for t in triplets)
l4mpi
  • 5,103
  • 3
  • 34
  • 54
1

Find all substrings of length 3 and check whether any are in the second string.

valid = not any(sub.group(1) in string2 for sub in re.finditer(r"(?=(.{3}))", string1))

Here is a demonstration: http://ideone.com/T77mHn

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
0

Well, problems of this kind (mind bending iteration) tend to have elegant functional solutions:

def not3(string2, string1):
    if len(string2) < 3:  return True    # reaching here, the sequence wasn't found
    if string2[:3] in string1: return False      # test beginning of string2 for existence in string1
    return not3(string2[1:], string1)  # recurse from next char of string2
Israel Unterman
  • 13,158
  • 4
  • 28
  • 35