modification of _strg for detecting valid FITS headers
- Dominant language
- Python
- Stars
- 5.3k
- Forks
- 2.2k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 75
Description
### Description
This is a continuation of [this issue](https://github.com/astropy/astropy/issues/14581). If there is a bare doubled-quote in a value, the regex will terminate there instead of at the end of the string. I see that there are ways that this is worked around but the following might allow better fidelity in parsing (and allow errors to be raised when there are not the correct number of paired single quotes within the outermost non-comment single quotes). The inline comment acknowledges that
```
# The regex is not correct for all cases, but
# it comes pretty darn close. It appears to find the
# end of a string rather well, but will accept
# strings with an odd number of single quotes,
# instead of issuing an error. The FITS standard
# appears vague on this issue and only states that a
# string should not end with two single quotes,
# whereas it should not end with an even number of
# quotes to be precise.
#
# Note that a non-greedy match is done for a string,
# since a greedy match will find a single-quote after
# the comment separator resulting in an incorrect
# match.
```
```python
>>> import re
>>> from astropy.io.fits import Card
>>> _strg = Card._strg
>>> re.match(_strg, "'a '' b'")
<-- truncated after first ''
>>> re.match(_strg, "'a ' b ' /c'")
<--truncated after first ' even though remainder is not comment
```
### Expected behavior
```python
>>> __strg="'(?P(?:[ -&(-~]|'')*)'(?= *(?:$|/))"
>>> re.match(__strg, "'a '' b'")
>>> re.match(__strg, "'a ' b ' /c'") is None
True
```
That string enforces that any single quotes in a string must appear doubled and that what appears after the last single quote is space followed by the end of the line or else a comment mark (forward slash).
### How to Reproduce
see above
### Versions
```python
import astropy
astropy.system_info()
```
```
platform
--------
platform.platform() = 'Windows-11-10.0.26100-SP0'
platform.version() = '10.0.26100'
platform.python_version() = '3.13.7'
packages
--------
astropy 7.1.1
numpy 2.2.3
scipy 1.15.2
matplotlib 3.10.1
pandas 2.2.3
pyerfa 2.0.1.5
```
Contributor guide
Research direction
Start at astropy.io.fits.Card._strg and reproduce the two regex examples from the issue. Verify that doubled quotes are retained through the closing quote and that an undoubled quote followed by non-comment text fails to match; add or update the relevant FITS card parsing tests if located nearby.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100