diff --git a/tornado/routing.py b/tornado/routing.py
index a35ac348..e137a728 100644
--- a/tornado/routing.py
+++ b/tornado/routing.py
@@ -627,7 +627,13 @@ class PathMatches(Matcher):
             if ")" in fragment:
                 paren_loc = fragment.index(")")
                 if paren_loc >= 0:
-                    pieces.append("%s" + fragment[paren_loc + 1 :])
+                    try:
+                        unescaped_fragment = re_unescape(fragment[paren_loc + 1 :])
+                    except ValueError:
+                        # If we can't unescape part of it, we can't
+                        # reverse this url.
+                        return (None, None)
+                    pieces.append("%s" + unescaped_fragment)
             else:
                 try:
                     unescaped_fragment = re_unescape(fragment)

reverse_url() unescapes regex inconsistently
If you have a rule with an escaped regex character coming after a group in parentheses, like:
```python
URLSpec('/project/([0-9]+)/export/codebook\\.csv', ExportCodebookCsv)
```
it will correctly match `/project/1/export/codebook.csv`, but will reverse to `/project/1/export/codebook\.csv`.

Of course, if you remove the backslash:
```python
URLSpec('/project/([0-9]+)/export/codebook.csv', ExportCodebookCsv)
```
it will reverse to the wanted `/project/1/export/codebook.csv` but then it will also match `/project/1/export/codebookXcsv` (the dot matches any character).

This is because `re_unescape()` is only called for the first fragment, not the following ones; see no unescaping line 630:
https://github.com/tornadoweb/tornado/blob/ff985fe509513325462441e017f1ec31dda737c1/tornado/routing.py#L626-L638

See also #1619
diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py
index 5908710a..0832f093 100644
--- a/tornado/test/web_test.py
+++ b/tornado/test/web_test.py
@@ -3107,6 +3107,10 @@ class URLSpecReverseTest(unittest.TestCase):
         self.assertEqual(
             "/api/v1/foo/bar", url(r"^/api/v1/foo/(\w+)$", None).reverse("bar")
         )
+        self.assertEqual(
+            "/api.v1/foo/5/icon.png",
+            url(r"/api\.v1/foo/([0-9]+)/icon\.png", None).reverse(5),
+        )
 
 
 class RedirectHandlerTest(WebTestCase):

pip install -e .[dev]
parse_log_pytest
pytest
pytest flake8 black mypy
apt-get update apt-get install -y build-essential python3-dev
3.7
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs @ file:///croot/attrs_1668696182826/work
black==23.3.0
certifi @ file:///croot/certifi_1671487769961/work/certifi
click==8.1.8
flake8==5.0.4
flit_core @ file:///opt/conda/conda-bld/flit-core_1644941570762/work/source/flit_core
importlib-metadata==4.2.0
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
mccabe==0.7.0
mypy==1.4.1
mypy-extensions==1.0.0
packaging @ file:///croot/packaging_1671697413597/work
pathspec==0.11.2
platformdirs==4.0.0
pluggy @ file:///tmp/build/80754af9/pluggy_1648042572264/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pycodestyle==2.9.1
pyflakes==2.5.0
pytest==7.1.2
tomli @ file:///opt/conda/conda-bld/tomli_1657175507142/work
-e git+https://github.com/tornadoweb/tornado.git@ff985fe509513325462441e017f1ec31dda737c1#egg=tornado
typed-ast==1.5.5
typing_extensions==4.7.1
zipp @ file:///croot/zipp_1672387121353/work

name: tornado
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=22.1.0=py37h06a4308_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - flit-core=3.6.0=pyhd3eb1b0_0
  - importlib_metadata=4.11.3=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=22.0=py37h06a4308_0
  - pip=22.3.1=py37h06a4308_0
  - pluggy=1.0.0=py37h06a4308_1
  - py=1.11.0=pyhd3eb1b0_0
  - pytest=7.1.2=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tomli=2.0.1=py37h06a4308_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.11.0=py37h06a4308_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - black==23.3.0
      - click==8.1.8
      - flake8==5.0.4
      - importlib-metadata==4.2.0
      - mccabe==0.7.0
      - mypy==1.4.1
      - mypy-extensions==1.0.0
      - pathspec==0.11.2
      - platformdirs==4.0.0
      - pycodestyle==2.9.1
      - pyflakes==2.5.0
      - typed-ast==1.5.5
      - typing-extensions==4.7.1
prefix: /opt/conda/envs/tornado

tornado/test/web_test.py::URLSpecReverseTest::test_reverse_arguments
tornado/test/web_test.py::SecureCookieV1Test::test_arbitrary_bytes tornado/test/web_test.py::SecureCookieV1Test::test_cookie_tampering_future_timestamp tornado/test/web_test.py::SecureCookieV1Test::test_round_trip tornado/test/web_test.py::SecureCookieV2Test::test_key_version_increment_version tornado/test/web_test.py::SecureCookieV2Test::test_key_version_invalidate_version tornado/test/web_test.py::SecureCookieV2Test::test_key_version_roundtrip tornado/test/web_test.py::SecureCookieV2Test::test_key_version_roundtrip_differing_version tornado/test/web_test.py::SecureCookieV2Test::test_round_trip tornado/test/web_test.py::FinalReturnTest::test_finish_method_return_future tornado/test/web_test.py::FinalReturnTest::test_render_method_return_future tornado/test/web_test.py::CookieTest::test_cookie_special_char tornado/test/web_test.py::CookieTest::test_get_cookie tornado/test/web_test.py::CookieTest::test_set_cookie tornado/test/web_test.py::CookieTest::test_set_cookie_domain tornado/test/web_test.py::CookieTest::test_set_cookie_expires_days tornado/test/web_test.py::CookieTest::test_set_cookie_false_flags tornado/test/web_test.py::CookieTest::test_set_cookie_max_age tornado/test/web_test.py::CookieTest::test_set_cookie_overwrite tornado/test/web_test.py::AuthRedirectTest::test_absolute_auth_redirect tornado/test/web_test.py::AuthRedirectTest::test_relative_auth_redirect tornado/test/web_test.py::ConnectionCloseTest::test_connection_close tornado/test/web_test.py::RequestEncodingTest::test_error tornado/test/web_test.py::RequestEncodingTest::test_group_encoding tornado/test/web_test.py::RequestEncodingTest::test_group_question_mark tornado/test/web_test.py::RequestEncodingTest::test_slashes tornado/test/web_test.py::WSGISafeWebTest::test_decode_argument tornado/test/web_test.py::WSGISafeWebTest::test_decode_argument_invalid_unicode tornado/test/web_test.py::WSGISafeWebTest::test_decode_argument_plus tornado/test/web_test.py::WSGISafeWebTest::test_get_argument tornado/test/web_test.py::WSGISafeWebTest::test_get_body_arguments tornado/test/web_test.py::WSGISafeWebTest::test_get_query_arguments tornado/test/web_test.py::WSGISafeWebTest::test_header_injection tornado/test/web_test.py::WSGISafeWebTest::test_multi_header tornado/test/web_test.py::WSGISafeWebTest::test_no_gzip tornado/test/web_test.py::WSGISafeWebTest::test_optional_path tornado/test/web_test.py::WSGISafeWebTest::test_redirect tornado/test/web_test.py::WSGISafeWebTest::test_reverse_url tornado/test/web_test.py::WSGISafeWebTest::test_types tornado/test/web_test.py::WSGISafeWebTest::test_uimodule_resources tornado/test/web_test.py::WSGISafeWebTest::test_uimodule_unescaped tornado/test/web_test.py::WSGISafeWebTest::test_web_redirect tornado/test/web_test.py::WSGISafeWebTest::test_web_redirect_double_slash tornado/test/web_test.py::NonWSGIWebTests::test_empty_flush tornado/test/web_test.py::ErrorResponseTest::test_default tornado/test/web_test.py::ErrorResponseTest::test_failed_write_error tornado/test/web_test.py::ErrorResponseTest::test_write_error tornado/test/web_test.py::StaticFileTest::test_absolute_static_url tornado/test/web_test.py::StaticFileTest::test_absolute_version_exclusion tornado/test/web_test.py::StaticFileTest::test_include_host_override tornado/test/web_test.py::StaticFileTest::test_path_traversal_protection tornado/test/web_test.py::StaticFileTest::test_relative_version_exclusion tornado/test/web_test.py::StaticFileTest::test_root_static_path tornado/test/web_test.py::StaticFileTest::test_static_304_etag_modified_bug tornado/test/web_test.py::StaticFileTest::test_static_304_if_modified_since tornado/test/web_test.py::StaticFileTest::test_static_304_if_none_match tornado/test/web_test.py::StaticFileTest::test_static_404 tornado/test/web_test.py::StaticFileTest::test_static_compressed_files tornado/test/web_test.py::StaticFileTest::test_static_etag tornado/test/web_test.py::StaticFileTest::test_static_files tornado/test/web_test.py::StaticFileTest::test_static_head tornado/test/web_test.py::StaticFileTest::test_static_head_range tornado/test/web_test.py::StaticFileTest::test_static_if_modified_since_pre_epoch tornado/test/web_test.py::StaticFileTest::test_static_if_modified_since_time_zone tornado/test/web_test.py::StaticFileTest::test_static_invalid_range tornado/test/web_test.py::StaticFileTest::test_static_range_if_none_match tornado/test/web_test.py::StaticFileTest::test_static_unsatisfiable_range_end_less_than_start tornado/test/web_test.py::StaticFileTest::test_static_unsatisfiable_range_invalid_start tornado/test/web_test.py::StaticFileTest::test_static_unsatisfiable_range_zero_suffix tornado/test/web_test.py::StaticFileTest::test_static_url tornado/test/web_test.py::StaticFileTest::test_static_with_range tornado/test/web_test.py::StaticFileTest::test_static_with_range_end_edge tornado/test/web_test.py::StaticFileTest::test_static_with_range_full_file tornado/test/web_test.py::StaticFileTest::test_static_with_range_full_past_end tornado/test/web_test.py::StaticFileTest::test_static_with_range_neg_end tornado/test/web_test.py::StaticFileTest::test_static_with_range_neg_past_start tornado/test/web_test.py::StaticFileTest::test_static_with_range_partial_past_end tornado/test/web_test.py::StaticDefaultFilenameTest::test_static_default_filename tornado/test/web_test.py::StaticDefaultFilenameTest::test_static_default_redirect tornado/test/web_test.py::StaticFileWithPathTest::test_serve tornado/test/web_test.py::CustomStaticFileTest::test_serve tornado/test/web_test.py::CustomStaticFileTest::test_static_url tornado/test/web_test.py::HostMatchingTest::test_host_matching tornado/test/web_test.py::DefaultHostMatchingTest::test_default_host_matching tornado/test/web_test.py::NamedURLSpecGroupsTest::test_named_urlspec_groups tornado/test/web_test.py::ClearHeaderTest::test_clear_header tornado/test/web_test.py::Header204Test::test_204_headers tornado/test/web_test.py::Header304Test::test_304_headers tornado/test/web_test.py::StatusReasonTest::test_status tornado/test/web_test.py::DateHeaderTest::test_date_header tornado/test/web_test.py::RaiseWithReasonTest::test_httperror_str tornado/test/web_test.py::RaiseWithReasonTest::test_httperror_str_from_httputil tornado/test/web_test.py::RaiseWithReasonTest::test_raise_with_reason tornado/test/web_test.py::ErrorHandlerXSRFTest::test_404_xsrf tornado/test/web_test.py::ErrorHandlerXSRFTest::test_error_xsrf tornado/test/web_test.py::GzipTestCase::test_gzip tornado/test/web_test.py::GzipTestCase::test_gzip_not_requested tornado/test/web_test.py::GzipTestCase::test_gzip_static tornado/test/web_test.py::GzipTestCase::test_vary_already_present tornado/test/web_test.py::GzipTestCase::test_vary_already_present_multiple tornado/test/web_test.py::PathArgsInPrepareTest::test_kw tornado/test/web_test.py::PathArgsInPrepareTest::test_pos tornado/test/web_test.py::ClearAllCookiesTest::test_clear_all_cookies tornado/test/web_test.py::ExceptionHandlerTest::test_http_error tornado/test/web_test.py::ExceptionHandlerTest::test_known_error tornado/test/web_test.py::ExceptionHandlerTest::test_unknown_error tornado/test/web_test.py::BuggyLoggingTest::test_buggy_log_exception tornado/test/web_test.py::UIMethodUIModuleTest::test_ui_method tornado/test/web_test.py::GetArgumentErrorTest::test_catch_error tornado/test/web_test.py::SetLazyPropertiesTest::test_set_properties tornado/test/web_test.py::GetCurrentUserTest::test_get_current_user_from_ui_module_is_lazy tornado/test/web_test.py::GetCurrentUserTest::test_get_current_user_from_ui_module_works tornado/test/web_test.py::GetCurrentUserTest::test_get_current_user_works tornado/test/web_test.py::UnimplementedHTTPMethodsTest::test_unimplemented_standard_methods tornado/test/web_test.py::UnimplementedNonStandardMethodsTest::test_unimplemented_other tornado/test/web_test.py::UnimplementedNonStandardMethodsTest::test_unimplemented_patch tornado/test/web_test.py::AllHTTPMethodsTest::test_standard_methods tornado/test/web_test.py::PatchMethodTest::test_other tornado/test/web_test.py::PatchMethodTest::test_patch tornado/test/web_test.py::FinishInPrepareTest::test_finish_in_prepare tornado/test/web_test.py::Default404Test::test_404 tornado/test/web_test.py::Custom404Test::test_404 tornado/test/web_test.py::DefaultHandlerArgumentsTest::test_403 tornado/test/web_test.py::HandlerByNameTest::test_handler_by_name tornado/test/web_test.py::StreamingRequestBodyTest::test_close_during_upload tornado/test/web_test.py::StreamingRequestBodyTest::test_early_return tornado/test/web_test.py::StreamingRequestBodyTest::test_early_return_with_data tornado/test/web_test.py::StreamingRequestBodyTest::test_streaming_body tornado/test/web_test.py::DecoratedStreamingRequestFlowControlTest::test_flow_control_chunked_body tornado/test/web_test.py::DecoratedStreamingRequestFlowControlTest::test_flow_control_compressed_body tornado/test/web_test.py::DecoratedStreamingRequestFlowControlTest::test_flow_control_fixed_body tornado/test/web_test.py::NativeStreamingRequestFlowControlTest::test_flow_control_chunked_body tornado/test/web_test.py::NativeStreamingRequestFlowControlTest::test_flow_control_compressed_body tornado/test/web_test.py::NativeStreamingRequestFlowControlTest::test_flow_control_fixed_body tornado/test/web_test.py::IncorrectContentLengthTest::test_content_length_too_high tornado/test/web_test.py::IncorrectContentLengthTest::test_content_length_too_low tornado/test/web_test.py::ClientCloseTest::test_client_close tornado/test/web_test.py::SignedValueTest::test_expired tornado/test/web_test.py::SignedValueTest::test_key_version_retrieval tornado/test/web_test.py::SignedValueTest::test_key_versioning_invalid_key tornado/test/web_test.py::SignedValueTest::test_key_versioning_read_write_default_key tornado/test/web_test.py::SignedValueTest::test_key_versioning_read_write_non_default_key tornado/test/web_test.py::SignedValueTest::test_known_values tornado/test/web_test.py::SignedValueTest::test_name_swap tornado/test/web_test.py::SignedValueTest::test_non_ascii tornado/test/web_test.py::SignedValueTest::test_payload_tampering tornado/test/web_test.py::SignedValueTest::test_signature_tampering tornado/test/web_test.py::XSRFTest::test_cross_user tornado/test/web_test.py::XSRFTest::test_distinct_tokens tornado/test/web_test.py::XSRFTest::test_refresh_token tornado/test/web_test.py::XSRFTest::test_versioning tornado/test/web_test.py::XSRFTest::test_xsrf_fail_argument_invalid_format tornado/test/web_test.py::XSRFTest::test_xsrf_fail_body_no_cookie tornado/test/web_test.py::XSRFTest::test_xsrf_fail_cookie_invalid_format tornado/test/web_test.py::XSRFTest::test_xsrf_fail_cookie_no_body tornado/test/web_test.py::XSRFTest::test_xsrf_fail_no_token tornado/test/web_test.py::XSRFTest::test_xsrf_success_header tornado/test/web_test.py::XSRFTest::test_xsrf_success_non_hex_token tornado/test/web_test.py::XSRFTest::test_xsrf_success_post_body tornado/test/web_test.py::XSRFTest::test_xsrf_success_query_string tornado/test/web_test.py::XSRFTest::test_xsrf_success_short_token tornado/test/web_test.py::XSRFCookieKwargsTest::test_xsrf_httponly tornado/test/web_test.py::FinishExceptionTest::test_finish_exception tornado/test/web_test.py::DecoratorTest::test_addslash tornado/test/web_test.py::DecoratorTest::test_removeslash tornado/test/web_test.py::CacheTest::test_multiple_strong_etag_match tornado/test/web_test.py::CacheTest::test_multiple_strong_etag_not_match tornado/test/web_test.py::CacheTest::test_multiple_weak_etag_match tornado/test/web_test.py::CacheTest::test_multiple_weak_etag_not_match tornado/test/web_test.py::CacheTest::test_strong_etag_match tornado/test/web_test.py::CacheTest::test_strong_etag_not_match tornado/test/web_test.py::CacheTest::test_weak_etag_match tornado/test/web_test.py::CacheTest::test_weak_etag_not_match tornado/test/web_test.py::CacheTest::test_wildcard_etag tornado/test/web_test.py::RequestSummaryTest::test_missing_remote_ip tornado/test/web_test.py::HTTPErrorTest::test_copy tornado/test/web_test.py::ApplicationTest::test_listen tornado/test/web_test.py::URLSpecReverseTest::test_non_reversible tornado/test/web_test.py::URLSpecReverseTest::test_reverse tornado/test/web_test.py::RedirectHandlerTest::test_basic_redirect tornado/test/web_test.py::RedirectHandlerTest::test_redirect_pattern tornado/test/web_test.py::RedirectHandlerTest::test_redirect_with_appending_argument tornado/test/web_test.py::RedirectHandlerTest::test_redirect_with_argument
tornado/routing.py
tornado/routing.py