-2

I'm trying to create a regexp that will capture the first occurrence of a repeating string.

The string is a "list" of RSA certificates , in succession. I am only interested in the first.

The string looks like:

-----BEGIN CERTIFICATE-----
CERT1..........
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
CERT2............
-----END CERTIFICATE-----
...
...
...

I've tried variations on the following , but it is returning the full list rather than the first item:

set match [regexp {(-----BEGIN CERTIFICATE-----.*-----END CERTIFICATE-----$).*} $certs matchAll firstCert]
Avba
  • 14,822
  • 20
  • 92
  • 192
  • The answers in the duplicate is either "non-greedy match" or "negative character class". The equivalent of the latter in this case is `{(-----BEGIN CERTIFICATE-----[^-]*-----END CERTIFICATE-----)}`. The `[^-]*` expression will match anything that isn't a dash. – Peter Lewerin May 08 '16 at 14:53
  • @PeterLewerin That suggestion seems too brittle. If there happens to be a '-' character in some certificate (maybe it's even allowed?) then it will not work as expected. – Brandin May 09 '16 at 09:39
  • @Brandin: yes. I just wanted to show the equivalent in this case. If it is inappropriate then it certainly shouldn't be used. If it is appropriate it's a viable alternative. – Peter Lewerin May 09 '16 at 11:33

1 Answers1

0

You need to use the non-greedy .*? quantifier.

set match [regexp {(-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----)} \
    $certs matchAll firstCert]

References re_syntax manual page

Brad Lanam
  • 5,192
  • 2
  • 19
  • 29