import re
regex = re.compile(r"^pin \"([^\"]*)\".* #.*@(\d+\.\d+\.\d+(?:[^\s]*)).*$", flags=re.MULTILINE)
test_str = ("# Pin npm packages by running ./bin/importmap\n\n"
"pin \"application\", preload: true\n"
"pin \"@hotwired/turbo-rails\", to: \"turbo.min.js\", preload: true\n"
"pin \"@hotwired/stimulus\", to: \"https://ga.jspm.io/npm:@hotwired/stimulus@3.0.1/dist/stimulus.js\"\n"
"pin \"@hotwired/stimulus-loading\", to: \"stimulus-loading.js\", preload: true\n"
"pin_all_from \"app/javascript/controllers\", under: \"controllers\"\n"
"pin \"react\", to: \"https://ga.jspm.io/npm:react@16.0.0/index.js\"\n"
"pin \"object-assign\" # @4.1.1\n"
"pin \"glob-parent\", to: \"https://ga.jspm.io/npm:glob-parent@3.1.0/index.js\"\n"
"pin \"is-extglob\", to: \"https://ga.jspm.io/npm:is-extglob@2.1.1/index.js\"\n"
"pin \"is-glob\", to: \"https://ga.jspm.io/npm:is-glob@3.1.0/index.js\"\n"
"pin \"os\", to: \"https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.18/nodelibs/browser/os.js\"\n"
"pin \"path\", to: \"https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.19/nodelibs/browser/path.js\"\n"
"pin \"path-dirname\", to: \"https://ga.jspm.io/npm:path-dirname@1.0.2/index.js\"\n"
"pin \"process\", to: \"https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.19/nodelibs/browser/process-production.js\"\n"
"pin \"util\", to: \"https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.2/nodelibs/browser/util.js\"\n"
"pin \"assastimulus\", to: \"https://ga.jspm.io/npm:stimulus@2.0.0/dist/stimulus.umd.js\"\n"
"pin \"stimulus\", to: \"https://ga.jspm.io/npm:stimulus@2.0.0/dist/stimulus.umd.js\"\n"
"pin \"lodash\" # @4.17.1 a comment\n"
"pin \"@github/hotkey\", to: \"@github--hotkey.js\" # @2.0.0\n"
"pin \"is-svg\", to: \"https://ga.jspm.io/npm:is-svg@3.0.0/index.js\"\n"
"pin \"buffer\", to: \"https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.19/nodelibs/browser/buffer.js\"\n"
"pin \"html-comment-regex\", to: \"https://ga.jspm.io/npm:html-comment-regex@1.1.2/index.js\"\n"
"pin \"nth-check\", to: \"https://ga.jspm.io/npm:nth-check@1.0.0/index.js\"\n"
"pin \"boolbase\", to: \"https://ga.jspm.io/npm:boolbase@1.0.0/index.js\"\n"
"pin \"jquery\", to: \"https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.js\"\n"
"pin \"angularjs\", to: \"https://unpkg.com/angularjs@0.0.1/index.js\"\n"
"pin \"angular\", to: \"https://unpkg.com/angular@1.8.2/index.js\"\n"
"pin \"@angular/cli\", to: \"https://unpkg.com/@angular/cli@13.2.5/lib/cli/index.js\"\n"
"pin \"underscore\" # a comment @1.13.2\n")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html