How to match specific characters only?

dhana

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 ?

DNA

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Regex: How to match sequence of SAME characters?

来自分类Dev

How to allow only specific alphabets in a Textbox?

来自分类Dev

How to allow only specific alphabets in a Textbox?

来自分类Dev

How to remove all characters from a string before a specific character

来自分类Dev

how to fetch database value and match it with user input value and display the match details only not other

来自分类Dev

Elegant way to use regex to match order-indifferent groups of characters (in a substring) while limiting how may times a given character can appear?

来自分类Dev

PHP Regex match specific site url with params

来自分类Dev

Match a string with number but only if there is > in front of the number

来自分类Dev

Is it possible to highlight only match_phrase query?

来自分类Dev

regexp to match characters not flowed by a character that is repeated an amount of times

来自分类Dev

OpenSSH server only listen to specific hostname

来自分类Dev

Select only n number of characters from VARCHAR(MAX) field

来自分类Dev

Why does Matlab only accept a small set of characters in script filenames?

来自分类Dev

Excel: Is it possible to copy cells only if cells match in other columns?

来自分类Dev

Return only specific fields from projection array sub-document

来自分类Dev

Joi validate array of objects, such that only one object has specific property

来自分类Dev

set bootstrap "required" attribute on textarea only if specific radio button selected

来自分类Dev

How to undo strsplit to put multiple characters into one

来自分类Dev

How to convert a series of unicode characters into readable text?

来自分类Dev

How to decode special characters in an NSURL in Swift?

来自分类Dev

How to insert a newline character to an array of characters

来自分类Dev

how to compare two strings ignoring few characters

来自分类Dev

How can I simply consume unrecognized characters?

来自分类Dev

How to search for special characters in Sublime Text 3

来自分类Dev

r language: how to find rows with match in list?

来自分类Dev

Mockito: How to match any enum parameter

来自分类Dev

How to dynamically create regex to use in .match Javascript?

来自分类Dev

How to get regex match on my scenario

来自分类Dev

Java Regex: How to Match URL Path?

Related 相关文章

  1. 1

    Regex: How to match sequence of SAME characters?

  2. 2

    How to allow only specific alphabets in a Textbox?

  3. 3

    How to allow only specific alphabets in a Textbox?

  4. 4

    How to remove all characters from a string before a specific character

  5. 5

    how to fetch database value and match it with user input value and display the match details only not other

  6. 6

    Elegant way to use regex to match order-indifferent groups of characters (in a substring) while limiting how may times a given character can appear?

  7. 7

    PHP Regex match specific site url with params

  8. 8

    Match a string with number but only if there is > in front of the number

  9. 9

    Is it possible to highlight only match_phrase query?

  10. 10

    regexp to match characters not flowed by a character that is repeated an amount of times

  11. 11

    OpenSSH server only listen to specific hostname

  12. 12

    Select only n number of characters from VARCHAR(MAX) field

  13. 13

    Why does Matlab only accept a small set of characters in script filenames?

  14. 14

    Excel: Is it possible to copy cells only if cells match in other columns?

  15. 15

    Return only specific fields from projection array sub-document

  16. 16

    Joi validate array of objects, such that only one object has specific property

  17. 17

    set bootstrap "required" attribute on textarea only if specific radio button selected

  18. 18

    How to undo strsplit to put multiple characters into one

  19. 19

    How to convert a series of unicode characters into readable text?

  20. 20

    How to decode special characters in an NSURL in Swift?

  21. 21

    How to insert a newline character to an array of characters

  22. 22

    how to compare two strings ignoring few characters

  23. 23

    How can I simply consume unrecognized characters?

  24. 24

    How to search for special characters in Sublime Text 3

  25. 25

    r language: how to find rows with match in list?

  26. 26

    Mockito: How to match any enum parameter

  27. 27

    How to dynamically create regex to use in .match Javascript?

  28. 28

    How to get regex match on my scenario

  29. 29

    Java Regex: How to Match URL Path?

热门标签

归档