You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BASH环境下符合特定规则的地址验证Regex构建需求

Hey there! Let's build that address validation regex for Bash that fits your exact requirements. I'll break down the logic, share the regex, and show you how to test it with your examples.

Bash Address Validation Regex

First, here's the full regex tailored to match your valid cases and reject the invalid ones:

^[0-9]+( [A-Z0-9][A-Za-z0-9.]+)+$

Regex Breakdown

Let's unpack each component so you understand exactly what it does:

  • ^ : Anchors the match to the start of the string (prevents leading invalid characters like letters before the number)
  • [0-9]+ : Matches at least 1 digit (fulfills your requirement for the pure-digit number segment, which can have any extra digits afterward)
  • ( [A-Z0-9][A-Za-z0-9.]+)+ : This handles the street/address details, broken down further:
    • : A mandatory space separating the number from the first street term
    • [A-Z0-9] : Each street term must start with an uppercase letter or digit
    • [A-Za-z0-9.]+ : Followed by one or more of uppercase letters, lowercase letters, digits, or periods
    • The outer + ensures there's at least one street term, and allows for multiple additional valid terms (like "Dr. Apt. 7" in your example)
  • $ : Anchors the match to the end of the string (blocks trailing invalid characters like extra spaces or symbols)

Testing in Bash

Bash uses extended regular expressions (ERE) with the [[ ... =~ ... ]] syntax. Here's a script to test the regex against your examples:

# Define test addresses
valid_addresses=(
    "123 Milk Ln."
    "15090 178th Ave"
    "27 Hilltop Dr. Apt. 7"
)
invalid_addresses=(
    "F52 Milk Ln."
    "156A Fantastic ..."
)

# Validate valid addresses
echo "Testing valid addresses:"
for addr in "${valid_addresses[@]}"; do
    if [[ $addr =~ ^[0-9]+( [A-Z0-9][A-Za-z0-9.]+)+$ ]]; then
        echo "✅ '$addr' is valid"
    else
        echo "❌ '$addr' is invalid (should be valid)"
    fi
done

# Validate invalid addresses
echo -e "\nTesting invalid addresses:"
for addr in "${invalid_addresses[@]}"; do
    if [[ $addr =~ ^[0-9]+( [A-Z0-9][A-Za-z0-9.]+)+$ ]]; then
        echo "❌ '$addr' is valid (should be invalid)"
    else
        echo "✅ '$addr' is invalid"
    fi
done

Why This Works for Your Examples

Valid Cases

  • "123 Milk Ln.": Starts with 3 digits, followed by valid terms ("Milk" starts with uppercase, "Ln." uses an allowed period)
  • "15090 178th Ave": 5-digit lead-in, plus "178th" (starts with a digit) and "Ave" (uppercase start)
  • "27 Hilltop Dr. Apt. 7": 2-digit lead-in, with multiple valid terms including "Apt. 7" (starts with uppercase, ends with a digit)

Invalid Cases

  • "F52 Milk Ln.": Starts with a letter instead of digits → fails the initial [0-9]+ check
  • "156A Fantastic ...": The "156A" segment mixes digits and a letter without a separating space, so it doesn't match the required digit-then-space structure

内容的提问来源于stack exchange,提问作者arashid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:07:24