正则表达式需求:提取文本中的CR-000000格式编号
Hey there, let's fix that regex issue you're having. Your original pattern didn't work because you used a character class [CR-] which matches individual characters (C, R, or -) instead of the literal sequence CR- you need. Here's the correct approach:
Extracting CR-XXXXXXX Format IDs
The right regex to capture all your CR- style IDs is:
CR-\d+
Let's break this down:
CR-: This matches the exact literal sequence "CR-" (no more mixing up characters with a class!)\d+: This grabs one or more digits, so it works for any length of numeric suffix (like the 7-digit IDs in your example)
If you know the numeric part will always be exactly 7 digits (like your sample), you can tighten it up to be more precise:
CR-\d{7}
How to get all matches:
Here's how to use this in common scenarios:
Python:
import re text = "The change Request has been created for the location below. CR-0001083 Click this link to access the Change Request Change Request ID : CR-0001086 Property ID: CK1014" matches = re.findall(r'CR-\d+', text) print(' '.join(matches)) # Outputs: CR-0001083 CR-0001086
JavaScript:
const text = "The change Request has been created for the location below. CR-0001083 Click this link to access the Change Request Change Request ID : CR-0001086 Property ID: CK1014"; const matches = text.match(/CR-\d+/g); console.log(matches.join(' ')); // Outputs: CR-0001083 CR-0001086
Command line (grep):
Use the -o flag to output only the matched parts:
grep -o "CR-\d+" your_file.txt
This will pull out every instance of your CR- IDs, ignoring unrelated content like the Property ID CK1014.
内容的提问来源于stack exchange,提问作者Danil Tk




