In bash, using regex, check if a string is a commit ID
- User
In bash, using regex, check if a string is a commit ID
Assistant
Here's how to check if a string is a Git commit ID using regex in bash:
# Check for full SHA-1 commit ID (40 hex characters) if [[ "$string" =~ ^[0-9a-fA-F]{40}$ ]]; then echo "Valid full commit ID" fi # Check for abbreviated or full SHA-1 commit ID (7-40 hex characters) if [[ "$string" =~ ^[0-9a-fA-F]{7,40}$ ]]; then echo "Valid commit ID (abbreviated or full)" fi # Check for SHA-256 commit ID (64 hex characters) - newer Git versions if [[ "$string" =~ ^[0-9a-fA-F]{64}$ ]]; then echo "Valid SHA-256 commit ID" fi # Combined check for any valid commit ID format if [[ "$string" =~ ^[0-9a-fA-F]{7,}$ ]] && [[ ${#string} -eq 40 || ${#string} -eq 64 || ${#string} -le 40 ]]; then echo "Valid commit ID" fi
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.