简体   繁体   中英

How to find a “piece” of word in a string with regex and using it in python?

For a holiday homework, in a password-check python program, I'd like to use regexs to find if the name (or a "piece" of the name, meaning 2 consecutive letters ) of the user is contained into a password he provided.

Part 1: the regex itself

Let's say the user is named Samuel :

  • password that would match:

    \nsamuel \nsAm123 \n123Sam \nlSAMUo \nsa \n
  • password that wouldn't match:

    \ns3m \nleumas \nsml \ns123 \n

So far, I've only succeeded to do this incomplete regex:

[sS]?[aA]?[mM]?[uU]?[eE]?[lL]?

But it matches if there is only one letter from the regex in the password and also when it's not 2 consecutive letters.

How can I enhance the regex?

Part 2: the python use

When I use the search function from re package, it only matches my previous regex if it's at the begening of the string. Here is a sample code of a case that should work:

import re
reg = r"[sS]?[aA]?[mM]?[uU]?[eE]?[lL]?"
re.match(reg, "samuel")  # Match
re.match(reg, "sml")  # Match
re.match(reg, "123sam")  # Doesn't match
re.match(reg, "zzzSAmu")  # Doesn't match

What am I missing?

You can try:

import re

pattern = r"(?i)sa|am|mu|ue|el"
for _string in ("samuel", "sAm123", "1235Sam", "lSAMUo", "sa"):
    assert re.search(pattern, _string) is not None

for _string in ("s3m", "leumas", "sml", "s123"):
    assert re.search(pattern, _string) is None

The main takes here are:

  • use re.search() rather than re.match() , as the documentation puts it:

    If you want to locate a match anywhere in string, use search() instead (see also search() vs. match()).

  • (?i) makes the regex case insensitive

Credits to @sin for suggesting using pairs of characters in the question's comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM