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

使用Sed正则表达式移除服务器名称中的UUID

Remove Embedded UUID from VM Server Names

Hey there! I totally get it—having random UUIDs cluttering up your server names is such a hassle when you need clean, readable identifiers. Since you mentioned the UUID has a fixed specific format, we can use regex to target and strip it out easily. Let's break this down for common tools you might be using day-to-day:

1. Python (great for bulk scripting)

If you're working with a list of server names in a script, Python's re module is perfect. Let's assume your UUID follows the standard 8-4-4-4-12 hex format (e.g., 123e4567-e89b-12d3-a456-426614174000):

import re

# Example VM name with embedded UUID
vm_name = "web-server-123e4567-e89b-12d3-a456-426614174000-prod"
# Regex pattern to match standard UUID
uuid_pattern = r'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'

# Strip the UUID
clean_name = re.sub(uuid_pattern, '', vm_name)
# Optional: Clean up extra hyphens left behind
clean_name = re.sub(r'--', '-', clean_name).strip('-')

print(clean_name)  # Output: web-server-prod

If your UUID has a different fixed format (like all uppercase, no hyphens, or a custom prefix), just adjust the regex pattern to match exactly what you're seeing.

2. Bash (command-line bulk processing)

If you're dealing with server names in a text file or via terminal, use sed with regex:

# Clean a single VM name example
echo "db-server-ABC123-DEF4-GHI5-JKL6-MNOPQRSTUVWX" | sed -E 's/[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}//g' | sed 's/--/-/g; s/^-//; s/-$//'

# Bulk process a file named vm_names.txt
sed -E 's/[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}//g' vm_names.txt | sed 's/--/-/g; s/^-//; s/-$//' > clean_vm_names.txt

3. PowerShell (Windows admin workflows)

For Windows environments, PowerShell's -replace operator handles this smoothly:

# Example VM name
$vmName = "app-server-98765432-10fe-dcba-5432-109876543210-dev"
# Regex pattern for UUID
$uuidPattern = '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'

# Clean the name
$cleanName = $vmName -replace $uuidPattern, ''
# Fix leftover extra hyphens
$cleanName = $cleanName -replace '--', '-' -replace '^-|-$', ''

Write-Output $cleanName  # Output: app-server-dev

Quick Customization Tip

If your UUID's format isn't the standard one, tweak the regex to match its exact structure:

  • 32-character hex without hyphens: use r'[0-9a-fA-F]{32}'
  • Prefixed with uuid-: use r'uuid-[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'

Just test the regex against a few sample VM names first to make sure it only targets the UUID and doesn't accidentally remove parts of your actual server name!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:29:37