dhana
dhana

Reputation: 6525

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 ?

Upvotes: 2

Views: 820

Answers (6)

DNA
DNA

Reputation: 42617

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)

Upvotes: 7

DNA
DNA

Reputation: 42617

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 (i.e. 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

Upvotes: 2

sanooj
sanooj

Reputation: 493

Should start with 2 or 3 followed by 2 or more occurrence of 2 or 3

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

Upvotes: 0

rvalvik
rvalvik

Reputation: 1559

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.

Upvotes: 3

hcarver
hcarver

Reputation: 7234

^(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.

Upvotes: 0

DominiCane
DominiCane

Reputation: 1303

Try this: (both 2 and 3 should exist)

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

Upvotes: 0

Related Questions