Blue Prism中个人身份码格式校验与异常处理需求
Got it, let's work through this validation problem step by step. Here's a robust approach to add checks before your queue processing kicks in, with meaningful business exceptions for invalid codes:
1. Define the Validation Rules with a Regular Expression
First, we’ll build a regex that enforces the exact DDMMYYSNNNX format and guarantees the code is 11 characters long. Let’s break down each segment to make it clear:
DD: Valid day (01-31) →(0[1-9]|[12][0-9]|3[01])MM: Valid month (01-12) →(0[1-9]|1[012])YY: Two-digit year (00-99) →\d{2}S: Either 'A' or '-' →[A\-](we escape the hyphen since it has special meaning in regex)NNN: Three-digit number →\d{3}X: Letter (case-insensitive) or number →[A-Za-z0-9]
Putting it all together, the full regex is:
^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])\d{2}[A\-]\d{3}[A-Za-z0-9]$
This regex inherently enforces the 11-character length because each segment adds up to exactly 11 characters.
2. Implement the Validation Logic
Here’s a concrete example using Java (adjust to your preferred language as needed) that includes non-empty checks, regex matching, and optional date validity verification (since a valid format doesn’t always mean a real date—like 3104... for April 31st):
// Custom business exception class (define this if you don't already have one) public class BusinessException extends Exception { public BusinessException(String message) { super(message); } } // Validation method to call before queue processing public void validatePersonalIdentityCode(String code) throws BusinessException { // First, check for null/empty values if (code == null || code.trim().isEmpty()) { throw new BusinessException("Personal Identity Code cannot be null or empty"); } String cleanedCode = code.trim(); // Apply the regex check String validationRegex = "^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])\\d{2}[A\\-]\\d{3}[A-Za-z0-9]$"; if (!cleanedCode.matches(validationRegex)) { throw new BusinessException( String.format("Invalid Personal Identity Code: %s. Must follow format DDMMYYSNNNX (examples: 310199-111K, 011206A2222)", cleanedCode) ); } // Optional: Validate that the date part is actually a real calendar date validateDateSegment(cleanedCode); } private void validateDateSegment(String code) throws BusinessException { int day = Integer.parseInt(code.substring(0, 2)); int month = Integer.parseInt(code.substring(2, 4)); int twoDigitYear = Integer.parseInt(code.substring(4, 6)); // Adjust two-digit year to full year based on your business rules (example logic below) int fullYear = twoDigitYear >= 50 ? 1900 + twoDigitYear : 2000 + twoDigitYear; try { // Use Java's LocalDate to validate the date exists LocalDate.of(fullYear, month, day); } catch (DateTimeException e) { throw new BusinessException( String.format("Invalid date in Personal Identity Code: %02d/%02d/%d", day, month, fullYear) ); } }
3. Integrate Validation Before Queue Processing
Right after extracting the Personal Identity Code from your Excel report (and before sending it to the queue), call the validatePersonalIdentityCode method. Catch the BusinessException to log the error, mark the invalid record, or notify stakeholders—whatever fits your workflow. This ensures only valid codes make it into the queue, preventing downstream issues.
Quick Adjustments for Your Use Case
- Case Insensitivity: If the 'A' in the
Ssegment or theXcharacter should be case-insensitive, add the(?i)flag at the start of the regex (e.g.,(?i)^(0[1-9]...)) or use case-insensitive matching in your code. - Year Handling: The two-digit year conversion logic (19xx vs 20xx) should align with your business’s rules—tweak that part if needed.
- Error Messages: Customize the exception messages to match your team’s terminology or user-facing requirements.
内容的提问来源于stack exchange,提问作者Rubi

