{"id":15664611,"url":"https://github.com/marioruiz/string_pattern","last_synced_at":"2026-03-05T07:01:44.937Z","repository":{"id":45006293,"uuid":"134281429","full_name":"MarioRuiz/string_pattern","owner":"MarioRuiz","description":"Generate strings supplying a simple pattern.  Perfect to be used in test data factories. Validate if a text fulfills a specific pattern.  Also you can use regular expressions (Regexp) to generate strings: `/[a-z0-9]{2,5}\\w+/.gen`. Generate words in English or Spanish. ","archived":false,"fork":false,"pushed_at":"2026-02-11T14:26:00.000Z","size":3146,"stargazers_count":17,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-02-22T21:56:36.457Z","etag":null,"topics":["data","error-detection","factories","generation","pattern","random","regex-pattern","regexp","regular-expressions","ruby","ruby-gem","string","test"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarioRuiz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-05-21T14:36:53.000Z","updated_at":"2026-02-11T14:22:34.000Z","dependencies_parsed_at":"2024-01-08T17:12:45.309Z","dependency_job_id":"598125a9-0dde-4c3a-b986-2e6a14d72f70","html_url":"https://github.com/MarioRuiz/string_pattern","commit_stats":{"total_commits":44,"total_committers":3,"mean_commits":"14.666666666666666","dds":0.06818181818181823,"last_synced_commit":"8f539e3bc6d7dd34838560503bba815914a01286"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/MarioRuiz/string_pattern","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioRuiz%2Fstring_pattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioRuiz%2Fstring_pattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioRuiz%2Fstring_pattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioRuiz%2Fstring_pattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarioRuiz","download_url":"https://codeload.github.com/MarioRuiz/string_pattern/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioRuiz%2Fstring_pattern/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30113111,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-05T03:40:26.266Z","status":"ssl_error","status_checked_at":"2026-03-05T03:39:15.902Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data","error-detection","factories","generation","pattern","random","regex-pattern","regexp","regular-expressions","ruby","ruby-gem","string","test"],"created_at":"2024-10-03T13:43:32.074Z","updated_at":"2026-03-05T07:01:44.919Z","avatar_url":"https://github.com/MarioRuiz.png","language":"Ruby","readme":"# StringPattern\n\n[![Gem Version](https://badge.fury.io/rb/string_pattern.svg)](https://rubygems.org/gems/string_pattern)\n[![Build Status](https://travis-ci.com/MarioRuiz/string_pattern.svg?branch=master)](https://github.com/MarioRuiz/string_pattern)\n[![Coverage Status](https://coveralls.io/repos/github/MarioRuiz/string_pattern/badge.svg?branch=master)](https://coveralls.io/github/MarioRuiz/string_pattern?branch=master)\n![Gem](https://img.shields.io/gem/dt/string_pattern)\n![GitHub commit activity](https://img.shields.io/github/commit-activity/y/MarioRuiz/string_pattern)\n![GitHub last commit](https://img.shields.io/github/last-commit/MarioRuiz/string_pattern)\n![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/MarioRuiz/string_pattern)\n\n\nWith this gem, you can easily generate strings supplying a very simple pattern. Even generate random words in English or Spanish.\nAlso, you can validate if a text fulfills a specific pattern or even generate a string following a pattern and returning the wrong length, value... for testing your applications. Perfect to be used in test data factories.\n\nAlso you can use regular expressions (Regexp) to generate strings: `/[a-z0-9]{2,5}\\w+/.gen`\n\nTo do even more take a look at [nice_hash gem](https://github.com/MarioRuiz/nice_hash)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'string_pattern'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install string_pattern\n\n## Usage\n\n### What is a string pattern?\n\nA pattern is a string where we supply these elements \"a-b:c\" where a is min_length, b is max_length (optional) and c is a set of symbol_type\n\n    min_length: minimum length of the string\n\t\n    max_length (optional): maximum length of the string. If not provided, the result will be with the min_length provided\n    \n\tsymbol_type: The type of the string we want.\n        x: from a to z (lowercase)\n        X: A to Z (capital letters)\n        L: A to Z and a to z\n        T: National characters defined on StringPattern.national_chars\n        n or N: for numbers. 0 to 9\n        $: special characters, $%\u0026#...  (includes blank space)\n        _: blank space\n        *: all characters\n        0: empty string will be accepted.  It needs to be at the beginning of the symbol_type string\n            @: It will generate a valid email following the official algorithm. It cannot be used with other symbol_type\n            W: for English words, capital and lower. It cannot be used with other symbol_type\n            w: for English words only lower and words separated by underscore. It cannot be used with other symbol_type\n            P: for Spanish words, capital and lower. It cannot be used with other symbol_type\n            p: for Spanish words only lower and words separated by underscore. It cannot be used with other symbol_type\n\t\t\n### How to generate a string following a pattern\n\nTo generate a string following a pattern you can do it using directly the StringPattern class or the generate method in the class, be aware you can always use also the alias method: gen\n\n```ruby\nrequire 'string_pattern'\n\n#StringPattern class\np StringPattern.generate \"10:N\"\n#\u003e3448910834\np StringPattern.gen \"5:X\"\n#\u003eJDDDK\n\n#String class\np \"4:Nx\".gen\n#\u003exaa3\n\n#Symbol class\np :\"10:T\".generate\n#\u003eAccBdjklñD\n\n#Array class\np [:\"3:N\", \"fixed\", :\"3:N\"].gen\n#\u003e334fixed920\np \"(,3:N,) ,3:N,-,2:N,-,2:N\".split(',').generate \n#\u003e(937) 980-65-05\n\n#Kernel\np gen \"3:N\"\n#\u003e443\n```\n\n#### Generating unique strings\n\nIf you want to generate for example 1000 strings and be sure all those strings are different you can use:\n\n```ruby\nStringPattern.dont_repeat = true #default: false\n1000.times {\n\tputs :\"6-20:L/N/\".gen\n}\nStringPattern.cache_values = Hash.new() #to clean the generated values from memory\n```\n\nUsing dont_repeat all the generated string during the current run will be unique.\n\nIn case you just want one particular string to be unique but not the rest then add to the pattern just in the end the symbol: \u0026\n\nThe pattern needs to be a symbol object.\n\n```ruby\n1000.times {\n\tputs :\"6-20:L/N/\u0026\".gen #will be unique\n\tputs :\"10:N\".gen\n}\n```\n\n#### Generate words randomly in English or Spanish\n\nTo generate a string of the length you want that will include only real words, use the symbol types:\n* W: generates English words following CamelCase ('ExampleOutput')\n* w: generates English words following snake_case ('example_output')\n* P: generates Spanish words following CamelCase ('EjemploSalida')\n* p: generates Spanish words following snake_case ('ejemplo_salida')\n\n```ruby\nrequire 'string_pattern'\n\nputs '10-30:W'.gen\n#\u003e FirstLieutenant\nputs '10-30:w'.gen\n#\u003e paris_university\nputs '10-30:P'.gen\n#\u003e SillaMetalizada\nputs '10-30:p'.gen\n#\u003e despacho_grande\n```\n\nIf you want to use a different word separator than \"_\" when using 'w' or 'p':\n\n```ruby\n# blank space for example\nrequire 'string_pattern'\n\nStringPattern.word_separator = ' '\n\nputs '10-30:w'.gen\n#\u003e paris university\nputs '10-30:p'.gen\n#\u003e despacho grande\n```\n\nThe word list is loaded on the first request to generate words, after that the speed to generate words increases amazingly. 85000 English words and 250000 Spanish words. The vocabularies are a sample of public open sources.\n\n#### Generate strings using Regular Expressions (Regexp)\n\nTake in consideration this feature is not supporting all possibilities for Regular expressions but it is fully functional. If you find any bug or limitation please add it to issues: https://github.com/MarioRuiz/string_pattern/issues\n\nIn case you want to change the default maximum for repetitions when using * or +: `StringPattern.default_infinite = 30` . By default is 10.\n\nIf you want to translate a regular expression into an StringPattern use the method we added to Regexp class: `to_sp`\n\nExamples:\n\n```ruby\n/[a-z0-9]{2-5}\\w+/.to_sp\n#\u003e [\"2-5:nx\", \"1-10:Ln_\"]\n\n#regular expression for UUID v4\n/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/.to_sp\n#\u003e [\"8:n[ABCDEF]\", \"-\", \"4:n[ABCDEF]\", \"-4\", \"3:n[ABCDEF]\", \"-\", \"1:[89AB]\", \"3:n[ABCDEF]\", \"-\", \"12:n[ABCDEF]\"]\n```\n\nIf you want to generate a random string following the regular expression, you can do it like a normal string pattern:\n\n```ruby\n\nregexp = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/\n\n# using StringPattern class\nputs StringPattern.generate(regexp)\n\n# using Kernel\nputs generate(regexp)\n\n# using generate method added to Regexp class\nputs regexp.generate\n\n#using the alias 'gen'\nputs regexp.gen \n\n# output:\n#\u003e7009574B-6F2F-436E-BB7A-EA5FDA6B4E47\n#\u003e5FB1718F-108A-4F62-8170-33C43FD86B1D\n#\u003e05745B6F-93BA-475F-8118-DD56E5EAC4D1\n#\u003e2D6FC189-8D50-45A8-B182-780193838502\n\n```\n\n### String patterns\n\n#### How to generate one or another string\n\nIn case you need to specify that the string is generated selecting one or another fixed string or pattern, you can do it by using Array of patterns and in the position you want you can add an array with the possible values\n\n```ruby\np [\"uno:\", :\"5:N\", ['.red','.green', :'3:L'] ].gen\n\n# first position a fixed string: \"uno:\"\n# second position 5 random numbers\n# third position one of these values: '.red', '.green' or 3 letters\n\n# example output: \n# 'uno:34322.red'\n# 'uno:44432.green'\n# 'uno:34322.red'\n# 'uno:28795xAB'\n\n```\n\nTake in consideration that this is only available to generate successful strings but not for validation\n\n#### Custom characters\n\nAlso, it's possible to provide the characters we want. To do that we'll use the symbol_type [characters]\n\nIf we want to add the character ] we have to write ]]\n\nExamples\n\n```ruby\n# four chars from the ones provided: asDF9\np \"4:[asDF9]\".gen    #\u003e aaaa, asFF, 9sFD\n\n# from 2 to 20 chars, capital and lower chars (Xx) and also valid the characters $#6\np \"2-20:[$#6]Xx\".gen    #\u003e aaaa, asFF, 66, B$DkKL#9aDD\n \n# four chars from these: asDF]9\np \"4:[asDF]]9]\".gen    #\u003e aa]a, asFF, 9s]D\n```\n\n#### Required characters or symbol types\n\nWe'll use the symbol / to specify which characters or symbols we want to be included on the resulting string as required values /symbols or characters/\n\nIf we need to add the character / we'll use //\n\nExamples:\n\n```ruby\n# four characters. optional: capitals and numbers, required: lower\n\"4:XN/x/\".gen    # aaaa, FF9b, j4em, asdf, ADFt\n\n# from 6 to 15 chars. optional: numbers, capitals and the chars $ and Æ. required the chars: 23abCD\n\"6-15:[/23abCD/$Æ]NX\".gen    # bCa$D32, 32DJIOKLaCb, b23aD568C\n \n# from 4 to 9 chars. optional: numbers and capitals. required: lowers and the characters $ and 5\n\"4-9:[/$5/]XN/x/\".generate    # aa5$, F5$F9b, j$4em5, a5sdf$, $ADFt5 \n```\n\n#### Excluded characters\n\nIf we want to exclude a few characters in the result, we'll use the symbol %characters%\n\nIf you need to exclude the character %, you should use %%\n\nExamples: \n\n```ruby\n# from 2 to 20 characters. optional: Numbers and characters A, B and C. excluded: the characters 8 and 3\n\"2-20:[%83%ABC]N\".gen    # B49, 22900, 9CAB, 22, 11CB6270C26C4572A50C\n\n# 10 chars. optional: Letters (capital and lower). required: numbers. excluded: the characters 0 and WXYzZ\n\"10:L/n/[%0WXYzZ%]\".gen    # GoO2ukCt4l, Q1Je2remFL, qPg1T92T2H, 4445556781\n```\n\n#### Not fulfilling a pattern\n\nIf we want our resulting string doesn't fulfill the pattern we supply, then we'll use the symbol ! at the beginning\n\nExamples:\n\n```ruby\n\"!4:XN/x/\".gen    # a$aaa, FF9B, j4DDDem, as, 2345\n\n\"!10:N\".gen     # 123, 34899Add34, 3434234234234008, AAFj#kd2x\n```\n\n### Generate a string with specific expected errors\n\nUsually, for testing purposes you need to generate strings that don't fulfill a specific pattern, then you can supply as a parameter expected_errors (alias: errors)\n\nThe possible values you can specify is one or more of these ones: :length, :min_length, :max_length, :value, :required_data, :excluded_data, :string_set_not_allowed\n\n    :length: wrong length, minimum or maximum\n    :min_length: wrong minimum length\n    :max_length: wrong maximum length\n    :value: wrong resultant value\n    :required_data: the output string won't include all necessary required data. It works only if required data supplied on the pattern.\n    :excluded_data: the resultant string will include one or more characters that should be excluded. It works only if excluded data supplied on the pattern.\n    :string_set_not_allowed: it will include one or more characters that are not supposed to be on the string.\n  \nExamples:\n\n```ruby\n\"10-20:N\".gen errors: [:min_length]\n#\u003e 627, 098262, 3408\n\n\"20:N\".gen errors: [:length, :value]\n#\u003e |13, tS1b)r-1)\u003cRT65202eTo6bV0g~, 021400323\u003c2ahL0NP86a698063*56076\n\n\"10:L/n/\".gen errors: [:value]\n#\u003e 1hwIw;v{KQ, mpk*l]!7:!, wocipgZt8@\n\n```\n\n### Validate if a string is following a pattern\n\nIf you need to validate if a specific text is fulfilling the pattern you can use the validate method.\n\nWhen you supply a single pattern and do **not** supply `expected_errors` or `not_expected_errors`, the method returns an **array of error symbols**: an empty array `[]` when the text is valid, or one or more of `:min_length`, `:max_length`, `:length`, `:value`, `:string_set_not_allowed`, `:required_data`, `:excluded_data` when invalid.\n\nWhen an array of patterns is supplied, the method returns only `true` or `false`.\n\nExamples:\n\n```ruby\n#StringPattern class\nStringPattern.validate((text: \"This text will be validated\", pattern: :\"10-20:Xn\")\n#\u003e [:max_length, :length, :value, :string_set_not_allowed]\n\n#String class\n\"10:N\".validate \"333444\"\n#\u003e [:min_length, :length]\n\n#Symbol class\n:\"10:N\".validate(\"333444\")\n#\u003e [:min_length, :length]\n\n#Array class\n[\"5:L\",\"3:xn\",\"4-10:n\"].validate \"DjkljFFc343444390\"\n#\u003e false\n```\n\nIf we want to validate a string with a pattern and we are expecting to get specific errors, you can supply the parameter expected_errors (alias: errors) or not_expected_errors (aliases: non_expected_errors, not_errors).\n\nIn this case, the validate method will return true or false.\n\nExamples: \n\n```ruby\n\"10:N\".val \"3445\", errors: [:min_length]\n#\u003e true\n\n\"10:N/[09]/\".validate \"4434039440\", errors: [:value]\n#\u003e false\n\n\"10-12:XN/x/\".validate \"FDDDDDAA343434\", errors: [:max_length, :required_data]\n#\u003e true\n```\n\n### Configure\n\n#### SP_ADD_TO_RUBY\n\nThis gem adds the methods generate (alias: gen) and validate (alias: val) to the Ruby classes: String, Array, and Symbol. \n\nAlso adds the method generate (alias: gen) to Kernel. By default (true) it is always added. \n\nIn case you don't want to be added, just before requiring the library set:\n\n```ruby\nSP_ADD_TO_RUBY = false\nrequire 'string_pattern'\n```\n\nIn case it is set to true (default) then you will be able to use:\n\n```ruby\nrequire 'string_pattern'\n\n#String object\n\"20-30:@\".gen \n#\u003edkj34MljjJD-df@jfdluul.dfu\n\n\"10:L/N/[/-./%d%]\".validate(\"12ds6f--.s\") \n#\u003e[:value, :string_set_not_allowed]\n\n\"20-40:@\".validate(my_email)\n\n#Kernel\ngen \"10:N\"\n#\u003e3433409877\n\n#Array object\n\"(,3:N,) ,3:N,-,2:N,-,2:N\".split(\",\").generate \n#\u003e(937) 980-65-05\n\n%w{( 3:N ) 1:_ 3:N - 2:N - 2:N}.gen \n#\u003e(045) 448-63-09\n\n[\"1:L\", \"5-10:LN\", \"-\", \"3:N\"].gen \n#\u003ezqWihV-746\n```\n\n#### national_chars\n\nTo specify which national characters will be used when using the symbol type: T, you use StringPattern.national_chars, by default is the English alphabet\n\n```ruby\nStringPattern.national_chars = (('a'..'z').to_a + ('A'..'Z').to_a).join + \"áéíóúÁÉÍÓÚüÜñÑ\"\n\"10-20:Tn\".gen #\u003eAAñ34Ef99éNOP\n```\n\n#### optimistic\n\nIf true it will check on the strings of the array positions supplied if they have the pattern format and assume in that case that is a pattern. If not it will assume the patterns on the array will be supplied as symbols. By default is set to true.\n\n```ruby\nStringPattern.optimistic = false\n[\"5:X\",\"fixedtext\", \"3:N\"].generate\n#\u003e5:Xfixedtext3:N\n[:\"5:X\",\"fixedtext\", :\"3:N\"].generate\n#\u003eAUJKJfixedtext454\n\nStringPattern.optimistic = true\n[\"5:X\",\"fixedtext\", \"3:N\"].generate\n#\u003eKKDMEfixedtext344\n[:\"5:X\",\"fixedtext\", :\"3:N\"].generate\n#\u003eSAAERfixedtext988\n```\n\n#### block_list\n\nTo specify which words will be avoided from the results\n\n```ruby\nStringPattern.block_list = ['example', 'wrong', 'ugly']\nStringPattern.block_list_enabled = true\n\"2-20:Tn\".gen #\u003eAAñ34Ef99éNOP\n```\n\n#### StringPattern.analyze\n\nTo inspect how a pattern is parsed without generating or validating:\n\n```ruby\np = StringPattern.analyze(\"10-20:LN/x/\")\n# =\u003e #\u003cStruct min_length=10, max_length=20, symbol_type=\"LN/x/\", required_data=..., string_set=..., unique=false\u003e\np.min_length   # =\u003e 10\np.max_length   # =\u003e 20\np.symbol_type  # =\u003e \"LN/x/\"\n```\n\nUseful for debugging or building tools on top of the pattern DSL. Invalid patterns return the pattern string; use `silent: true` to avoid logging.\n\n#### Error handling and logging\n\nBy default, when generation is impossible (e.g. invalid pattern or `dont_repeat` exhausted), `generate` returns an empty string `\"\"` and a message is printed. You can:\n\n- Set `StringPattern.logger = Logger.new($stderr)` to send messages to a logger instead of `puts`.\n- Set `StringPattern.raise_on_error = true` to raise `StringPattern::GenerationImpossibleError` or `StringPattern::InvalidPatternError` instead of returning `\"\"`.\n\n#### Reproducible generation (seed)\n\nPass `seed:` to get the same string for the same pattern in tests:\n\n```ruby\n\"10:N\".gen(seed: 42)  # =\u003e same result every time\n```\n\n#### Batch generation (sample)\n\nGenerate up to `n` distinct strings without mutating the global dont_repeat cache:\n\n```ruby\nStringPattern.sample(\"4:N\", 10)  # =\u003e array of 10 distinct 4-digit strings\n```\n\n#### Boolean validation (valid?)\n\nCheck if text matches a pattern without building the full error list:\n\n```ruby\nStringPattern.valid?(text: \"user@domain.com\", pattern: \"14-40:@\")  # =\u003e true\n```\n\n#### UUID\n\nGenerate a random UUID v4 or validate one:\n\n```ruby\nStringPattern.uuid                    # =\u003e \"550e8400-e29b-41d4-a716-446655440000\"\nStringPattern.valid_uuid?(some_str)   # =\u003e true or false\n```\n\n#### block_list as Proc\n\nYou can set `block_list` to a Proc for custom blocking:\n\n```ruby\nStringPattern.block_list = -\u003e(s) { s.include?(\"forbidden\") }\nStringPattern.block_list_enabled = true\n```\n\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/string_pattern.\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarioruiz%2Fstring_pattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarioruiz%2Fstring_pattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarioruiz%2Fstring_pattern/lists"}