{"id":341,"date":"2025-04-29T10:06:02","date_gmt":"2025-04-29T01:06:02","guid":{"rendered":"https:\/\/itexplore.org\/jp\/?p=341"},"modified":"2025-04-29T10:06:05","modified_gmt":"2025-04-29T01:06:05","slug":"is-alphanumeric-regex-in-python","status":"publish","type":"post","link":"https:\/\/itexplore.org\/jp\/tips\/is-alphanumeric-regex-in-python\/","title":{"rendered":"Python\u3067\u6587\u5b57\u5217\u304c\u534a\u89d2\u82f1\u6570\u5b57\u3067\u3042\u308b\u304b\u3092\u6b63\u898f\u8868\u73fe\u3067\u30c1\u30a7\u30c3\u30af\u3059\u308b"},"content":{"rendered":"<p>\u534a\u89d2\u82f1\u6570\u5b57\u3092\u6b63\u898f\u8868\u73fe\u3067\u30c1\u30a7\u30c3\u30af\u3059\u308b\u306b\u306f<code><span style=\"background: linear-gradient(transparent 60%,rgba(255, 253, 107, 0.7) 0);\" class=\"vk_highlighter\"><strong>^[a-zA-Z0-9]+$<\/strong><\/span><\/code>\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u306e\u6b63\u898f\u8868\u73fe\u306f\u300c1\u6587\u5b57\u4ee5\u4e0a\u306e\u534a\u89d2\u82f1\u6570\u5b57\u3067\u69cb\u6210\u3055\u308c\u308b\u6587\u5b57\u5217\u300d\u3068\u3044\u3046\u610f\u5473\u3092\u6301\u3061\u30013DModel \u306a\u3069\u306e\u6587\u5b57\u5217\u306b\u30de\u30c3\u30c1\u3057\u307e\u3059\u3002<\/p><p>\u3055\u3089\u306b\u3001\u56fa\u5b9a\u9577\u306e\u534a\u89d2\u82f1\u6570\u5b57\u3092\u6307\u5b9a\u3057\u305f\u3044\u5834\u5408\u306f<code><span>^[a-zA-Z0-9]{n}$<\/span><\/code>\u3001\u4f8b\u3048\u30706\u6841\u306e\u5834\u5408\u306f<code><span>^[a-zA-Z0-9]{6}$<\/span><\/code>\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u540c\u69d8\u306b\u3001\u6700\u5c0f\u6841\u6570\u3084\u6700\u5927\u6841\u6570\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u67d4\u8edf\u306a\u30d1\u30bf\u30fc\u30f3\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u300112\u6841\u4ee5\u4e0a20\u6841\u4ee5\u4e0b\u306b\u5236\u9650\u3057\u305f\u3044\u5834\u5408\u306f<code><span>^[a-zA-Z0-9]{12,20}$<\/span><\/code>\u304c\u6709\u52b9\u3067\u3059\u3002<\/p><p>\u4ee5\u4e0b\u306b\u3088\u304f\u5229\u7528\u3055\u308c\u308b\u6b63\u898f\u8868\u73fe\u30d1\u30bf\u30fc\u30f3\u3092\u307e\u3068\u3081\u307e\u3057\u305f\u3002\u7528\u9014\u306b\u5fdc\u3058\u3066\u3054\u6d3b\u7528\u304f\u3060\u3055\u3044\u3002<\/p><figure class=\"wp-block-table\"><table><tbody><tr><th>#<\/th><th>\u30de\u30c3\u30c1\u6761\u4ef6<\/th><th>\u6b63\u898f\u8868\u73fe\u30d1\u30bf\u30fc\u30f3<\/th><\/tr><tr><td style=\"text-align: right;\">1<\/td><td>\u3059\u3079\u3066\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]+$<\/td><\/tr><tr><td style=\"text-align: right;\">2<\/td><td>\u56fa\u5b9a\u9577 n\u6841\u306e\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]{n}$<\/td><\/tr><tr><td style=\"text-align: right;\">3<\/td><td>n\u6841\u4ee5\u4e0a\u306e\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]{n,}$<\/td><\/tr><tr><td style=\"text-align: right;\">4<\/td><td>m\u6841\u4ee5\u4e0b\u306e\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]{1,m}$<\/td><\/tr><tr><td style=\"text-align: right;\">5<\/td><td>n\u6841\u4ee5\u4e0am\u6841\u4ee5\u4e0b\u306e\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]{n,m}$<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">\u6b63\u898f\u8868\u73fe\u306e\u30d1\u30bf\u30fc\u30f3\u4e00\u89a7\u3068\u6761\u4ef6<\/figcaption><\/figure><h2>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2><p>\u6b21\u306b\u3001\u5165\u529b\u3055\u308c\u305f\u6587\u5b57\u5217\u304c\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u304b\u3092\u5224\u5b9a\u3059\u308bPython\u95a2\u6570\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002\u3053\u306e\u95a2\u6570\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u6761\u4ef6\u306b\u5fdc\u3058\u3066\u691c\u8a3c\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p><ul><li><strong>\u6700\u5c0f\u6841\u6570\u3092\u7701\u7565\u3057\u305f\u5834\u5408\uff1a<\/strong> \u6587\u5b57\u5217\u304c\u6307\u5b9a\u3055\u308c\u305f\u6700\u5927\u6841\u6570\u4ee5\u5185\u3067\u3001\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/li><li><strong>\u6700\u5927\u6841\u6570\u3092\u7701\u7565\u3057\u305f\u5834\u5408\uff1a<\/strong> \u6587\u5b57\u5217\u304c\u6307\u5b9a\u3055\u308c\u305f\u6700\u5c0f\u6841\u6570\u4ee5\u4e0a\u3067\u3001\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/li><li><strong>\u6700\u5c0f\u6841\u6570\u30fb\u6700\u5927\u6841\u6570\u3092\u7701\u7565\u3057\u305f\u5834\u5408\uff1a<\/strong> \u6587\u5b57\u5217\u5168\u4f53\u304c\u3001\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/li><\/ul><div class=\"input-field-container source-code-container\"><pre class=\"wp-block-code\"><code>import re\n\ndef validate_args_type(string, min_length, max_length):\n    \"\"\"\u5f15\u6570\u306e\u578b\u3092\u691c\u8a3c\u3057\u307e\u3059\"\"\"\n    if not isinstance(string, str):\n        raise TypeError(\"\u6587\u5b57\u5217 (string) \u306f str \u578b\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\")\n\n    if min_length is not None and not isinstance(min_length, int):\n        raise TypeError(\"\u6700\u5c0f\u6841\u6570 (min_length) \u306f int \u578b\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\")\n\n    if max_length is not None and not isinstance(max_length, int):\n        raise TypeError(\"\u6700\u5927\u6841\u6570 (max_length) \u306f int \u578b\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\")\n\ndef is_alphanumeric(string, min_length=None, max_length=None):\n    \"\"\"\n    \u6587\u5b57\u5217\u304c\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002\n\n    Args:\n        string (str): \u5165\u529b\u6587\u5b57\u5217\n        min_length (int or None): \u6700\u5c0f\u6841\u6570 (None\u306e\u5834\u5408\u306f1\u3068\u3057\u3066\u6271\u3044\u307e\u3059)\n        max_length (int or None): \u6700\u5927\u6841\u6570 (None\u306e\u5834\u5408\u306f\u6587\u5b57\u5217\u306e\u9577\u3055\u3092\u6700\u5927\u5024\u3068\u3057\u3066\u6271\u3044\u307e\u3059)\n\n    Returns:\n        bool: \u5165\u529b\u6587\u5b57\u5217\u304c\u6761\u4ef6\u3092\u6e80\u305f\u3059\u5834\u5408\u306fTrue\u3001\u305d\u308c\u4ee5\u5916\u306fFalse\u3092\u8fd4\u3057\u307e\u3059\u3002\n    \"\"\"\n    # \u5f15\u6570\u306e\u578b\u691c\u8a3c\n    validate_args_type(string, min_length, max_length)\n\n    # \u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u8a2d\u5b9a\n    min_len = min_length or 1\n    max_len = max_length or len(string)\n\n    # \u6700\u5c0f\u5024\u30fb\u6700\u5927\u5024\u306e\u6574\u5408\u6027\u30c1\u30a7\u30c3\u30af\n    if min_len < 1:\n        raise ValueError(\"\u6700\u5c0f\u6841\u6570 (min_length) \u306f1\u4ee5\u4e0a\u306e\u6574\u6570\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\")\n\n    if max_length is not None and max_len < min_len:\n        raise ValueError(\"\u6700\u5927\u6841\u6570 (max_length) \u306f\u6700\u5c0f\u6841\u6570\u4ee5\u4e0a\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\")\n\n    # \u6b63\u898f\u8868\u73fe\u30d1\u30bf\u30fc\u30f3\u3092\u69cb\u7bc9\n    if max_length is None:\n        pattern = re.compile(f\"^[a-zA-Z0-9]{{{min_len},}}$\")\n    else:\n        pattern = re.compile(f\"^[a-zA-Z0-9]{{{min_len},{max_len}}}$\")\n\n    # \u6b63\u898f\u8868\u73fe\u306b\u3088\u308b\u30c1\u30a7\u30c3\u30af\n    return bool(pattern.match(string))\n<\/code><\/pre><a id=\"copy-source-code-Python-is_alphanumeric\" class=\"action-button enable copy-source-code\" title=\"\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3057\u307e\u3059\u3002\"><i class=\"fa-regular fa-copy\"><\/i><\/a><\/div><p class=\"border-spacer\"><\/p><h3 class=\"wp-block-heading\">\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9<\/h3><div class=\"input-field-container source-code-container\"><pre class=\"wp-block-code\"><code># True: \u6761\u4ef6\u3092\u6e80\u305f\u3059\nprint(is_alphanumeric(\"hello123\", min_length=3, max_length=10))\n\n# False: \u6700\u5c0f\u6841\u6570\u672a\u6e80\nprint(is_alphanumeric(\"hello123\", min_length=10))\n\n# False: \u534a\u89d2\u82f1\u6570\u5b57\u4ee5\u5916\u3092\u542b\u3080\nprint(is_alphanumeric(\"hello!123\", min_length=1))\n\n# False: \u6700\u5927\u6841\u6570\u3092\u8d85\u3048\u308b\nprint(is_alphanumeric(\"abc\", max_length=2))\n\n# True: \u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3001\u6761\u4ef6\u3092\u6e80\u305f\u3059\nprint(is_alphanumeric(\"ABC123\", min_length=1, max_length=6))\n\n# False: \u7a7a\u6587\u5b57\u306f\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\nprint(is_alphanumeric(\"\", min_length=1))\n<\/code><\/pre><a id=\"copy-source-code-Python-is_alphanumeric_testcases\" class=\"action-button enable copy-source-code\" title=\"\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3057\u307e\u3059\u3002\"><i class=\"fa-regular fa-copy\"><\/i><\/a><\/div><p class=\"border-spacer\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u534a\u89d2\u82f1\u6570\u5b57\u3092\u6b63\u898f\u8868\u73fe\u3067\u30c1\u30a7\u30c3\u30af\u3059\u308b\u306b\u306f ^[a-zA-Z0-9]+$ \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u306e\u6b63\u898f\u8868\u73fe\u306f\u300c1\u6587\u5b57\u4ee5\u4e0a\u306e\u534a\u89d2\u82f1\u6570\u5b57\u3067\u69cb\u6210\u3055\u308c\u308b\u6587\u5b57\u5217\u300d\u3068\u3044\u3046\u610f\u5473\u3092\u6301\u3061\u30013DModel \u306a\u3069\u306e\u6587\u5b57\u5217\u306b\u30de\u30c3\u30c1\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":163,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"vkexunit_cta_each_option":"","footnotes":""},"categories":[4],"tags":[13,10],"class_list":["post-341","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips","tag-python","tag-10"],"_links":{"self":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts\/341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/comments?post=341"}],"version-history":[{"count":1,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":343,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts\/341\/revisions\/343"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/media\/163"}],"wp:attachment":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/media?parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/categories?post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/tags?post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}