简体   繁体   中英

How to match specific characters only?

Here I am try to match the specific characters in a string,

^[23]*$ 

Here my cases,

  1. 2233 --> Match
  2. 22 --> Not Match
  3. 33 --> Not Match
  4. 2435 --> Not Match
  5. 2322 --> Match
  6. 323 --> Match

I want to match the string with correct regular expression. I mean 1,5,6 cases needed.

Update:

If I have more than two digits match, like the patterns,

234 or 43 or etc. how to match this pattern with any string ?.

I want dynamic matching ?

How about:

(2+3|3+2)[23]*$

String must either:

  • start with one or more 2s, contain a 3, followed by any mix of 2 or 3 only
  • start with one or more 3s, contain a 2, followed by any mix of 2 or 3 only

Update: to parameterize the pattern

To parameterize this pattern, you could do something like:

x = 2
y = 3
pat = re.compile("(%s+%s|%s+%s)[%s%s]*$" % (x,y,y,x,x,y))
pat.match('2233')

Or a bit clearer, but longer:

pat = re.compile("({x}+{y}|{y}+{x})[{x}{y}]*$".format(x=2, y=3))

Or you could use Python template strings

Update: to handle more than two characters:

If you have more than two characters to test, then the regex gets unwieldy and my other answer becomes easier:

def match(s,ch):
    return all([c in s for c in ch]) and len(s.translate(None,ch)) == 0

match('223344','234') # True
match('2233445, '234') # False

Another update: use sets

I wasn't entirely happy with the above solution, as it seemed a bit ad-hoc. Eventually I realized it's just a set comparison - we just want to check that the input consists of a fixed set of characters:

def match(s,ch):
    return set(s) == set(ch)

If you want to match strings containing both 2 and 3, but no other characters you could use lookaheads combined with what you already have:

^(?=.*2)(?=.*3)[23]*$

The lookaheads (?=.*2) and (?=.*3) assert the presence of 2 and 3, and ^[23]*$ matches the actual string to only those two characters.

I know you asked for a solution using regex (which I have posted), but here's an alternative approach:

def match(s):
  return '2' in s and '3' in s and len(s.translate(None,'23')) == 0

We check that the string contains both desired characters, then translate them both to empty strings, then check that there's nothing left (ie we only had 2s and 3s).

This approach can easily be extended to handle more than two characters, using the all function, and a list comprehension:

def match(s,ch):
    return all([c in s for c in ch]) and len(s.translate(None,ch)) == 0

which would be used as follows:

match('223344','234') # True
match('2233445, '234') # False

试试这个:( 2和3都应该存在)

^([2]+[3]+[23]*)|([3]+[2]+[23]*)$
^(2[23]*3[23]*)|(3[23]*2[23]*)$

I think this will do it. Look for either a 2 at the start, then a 3 has to appear somewhere (surrounded by as many other 2s and 3s as needed). Or vice versa, with 3 at the start and a 2 somewhere.

应从2或3开始,然后2或更多次出现2或3

    ^[23][23]{2,}$

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