简体   繁体   中英

Python Regex for string matching

There are 2 rules that I am trying to match with regex . I've tried testing on various cases, giving me unwanted results.

Rules are as follows:

  1. Find all strings that are numbers (integer, decimal, and negative values included)
  2. Find all strings that have no numeric value. This is referring to special characters like !@#$%^&*()

So in my attempt to match these rules, I got this:

def rule(word): 
  if re.match("\W", word):
    return True
  elif re.match("[-.\d]", word):
    return True
  else:
    return False

Input: output tests are as follows

word = '972.2' : True

word = '-88.2' : True

word = '8fdsf' : True

word = '86fdsf' : True I want this to be False

word = '&^(' : True

There were some more tests, but I just wanted to show that one I want to return False. It seems like it's matching just the first character, so I tried changing the regex epressions, but that made things worse.

As the documentation says, re.match will return a MatchObject which always evaluates to True whenever the start of the string is matched to the regex.

Thus, you need to use anchors in regex to make sure only whole string match counts, eg [-.\\d]$ (note the dollar sign).

EDIT: plus what Max said - use + so your regex won't just match a single letter.

Your regexes (both of them) only look at the first character of your string. Change them to add +$ at the end in order to make sure your string is only made of the target characters.

As i understand, you need to exclude all except 1 and 2.

Try this:

import re

def rule(word):
  return True if re.search("[^\W\d\-\.]+", word) is None else False

Results on provided samples:

  • 972.2: True
  • -88.2: True
  • 8fdsf: False
  • 86fdsf: False
  • &^(: True

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