2011-10-18 4 views
0

동적 데이터를 사용하고 고유 키와 다른 데이터베이스 수준 유효성 검사를 적용하기 위해 SubmitChanges 함수를 재정의했습니다.정규식을 통해 값 추출

이렇게하면 최종 사용자에게 보여줄 수있는 원래의 오류가 발생합니다. 그러나,이 메시지는 다음과 같이이다 :

Violation of UNIQUE KEY constraint 'UK_CountryName_Country'. Cannot insert duplicate key in object 'dbo.Country'. The duplicate key value is (America). 
The statement has been terminated. 

나는 메시지의 다음 키워드가 필요합니다

  1. UNIQUE KEY
  2. UK_CountryName_Country 나는 다음과 같은 쓴
  3. 미국

을 정규식 :

System.Text.RegularExpressions.Regex.Split(e.Message.ToString(), "^Violation of (UNIQUE KEY){1} constraint '([a-zA-Z_]*)'. Cannot insert duplicate key in object 'dbo.([a-zA-Z]*)'. The duplicate key value is ") 

# 1 & # 2, 그러나 # 3을 얻는 데 성공했습니다.

아무도 도와 줄 수 있습니까? 또한, 그것을 달성하기위한 더 깨끗한 방법이 있습니까? FYI : 나는 다른 종류의 데이터베이스 오류도 잡을 것이다.

감사

답변

1

예를 들어 ression, 당신 대신 당신이 요청으로 Americadbo.Country에서 Country 일치하려고했던 듯, 그래서 모두 내 표현/코드에 포함 된 표현은 오류 유형의 매우 안전을 위해 디자인

Dim ExpressionText As String = "^Violation of (?<KeyType>.*?) constraint '(?<ConstraintName>[^']*)'\.[^']*'(?<ObjectName>[^']*)'[^(]*\((?<KeyValue>[^)]*)\)" 
Dim SearchText As String = "Violation of UNIQUE KEY constraint 'UK_CountryName_Country'. Cannot insert duplicate key in object 'dbo.Country'. The duplicate key value is (America). " _ 
           & vbCrLf & "The statement has been terminated." 
Dim regex As New System.Text.RegularExpressions.Regex(ExpressionText) 
Dim mc As System.Text.RegularExpressions.Match = regex.Match(SearchText) 

Response.Write(mc.Groups("KeyType").Value)   ' writes UNIQUE KEY 
Response.Write(mc.Groups(1).Value)     ' writes UNIQUE KEY 
Response.Write(mc.Groups("ConstraintName").Value) ' writes UK_CountryName_Country 
Response.Write(mc.Groups(2).Value)     ' writes UK_CountryName_Country 
Response.Write(mc.Groups("ObjectName").Value)  ' writes dbo.Country 
Response.Write(mc.Groups(3).Value)     ' writes dbo.Country 
Response.Write(mc.Groups("KeyValue").Value)   ' writes America 
Response.Write(mc.Groups(4).Value)     ' writes America 

메시지에는 위반할 수있는 다른 유형의 키 제약이 있으므로 나는 가능한 오류의 목록을 찾을 수 있지만, 여기에 표현의 고장입니다 없습니다 : 당신은 이름을 캡처 그룹을 사용하지 않으려면 ?<Something>처럼 보이는 부분 제거 할 수

^Violation of     # Match literal text 
(?<KeyType>.*?) constraint  # Capture the words that come before " constraint" 
'(?<ConstraintName>[^']*)'\. # Capture whatever is inside the single quotes: ' 
[^']*       # Match anything up to the next single quote 
'(?<ObjectName>[^']*)'   # Capture whatever is inside the single quotes 
[^(]*       # Match anything up to the next open parentheses: ( 
\((?<KeyValue>[^)]*)\)   #Capture whatever is inside the parentheses 

하고, 단지 숫자를 사용하고 싶습니다

+0

누군가 당신이 멋진 Regex 제작자라고 말 했나요? 첫 번째 시도에서의 성공. Regex를 배우기위한 링크를 안내해 줄 수 있습니까? 감사. – iMatoria

+0

아주 좋은 튜토리얼 (내가 정규식에 대해 아는 것의 90 %를 가르쳐 준다)은 [Regular-Expressions.info] (http://www.regular-expressions.info)에있다. - 사이트의 저자는 또한 에뮬레이트하고 모든 종류의 Regexes (모든 종류의 언어/구현 또는 흔히 "정규 표현식"이라고도 함)를 디버깅하는 데 탁월한 RegexBuddy라는 소프트웨어가 있습니다. –

1

마지막 값은 그냥 라인의 끝에 일치하는 최종 기간과 일치하지 않도록주의 할 수 끝 부분이기 때문에 :

".... The duplicate key value is (.*)\.$" 
+0

참으로 참되고 멋지 네요. 나는이 오류를 잡기 위해 다른 작업을 찾고있다. 나는 그것을 얻지 못하면 대답으로 표시 할 것입니다. 대답 해줘서 고마워. 매우 감사. – iMatoria

0
^Violation\s+of\s+(.*?)'(.*?)'.*?\((.*)\) 

내가 모르고 오전 가능한 모든 오류가 어떻게 생겼는지를 보여 주지만 위의 정규식은 다음과 같이 캡처합니다.

Group 1: UNIQUE KEY constraint  
Group 2: UK_CountryName_Country 
Group 3: America