2013-06-12 3 views
2

글자 또는 숫자로 된 숫자를 텍스트로 추출해야합니다.sas와 정확히 일치하는 perl 정규식

나는 그렇게 보이는 테이블,

... 1 child ... 
... three children ... 
...four children ...  
...2 children... 
...five children 

나는 단어 또는 숫자 숫자로 작성된 숫자를 캡처 할 수 있습니다. 한 줄에 하나의 번호가 있습니다. 그래서 원하는 출력은 다음과 같습니다

1 
three 
four 
2 
five 

내 정규식은 다음과 같습니다

prxparse("/one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|child|\d\d?/") 

어떤 도움?

+0

"정확히 하나 또는 두 개 또는 두 개 또는 네 개를 일치시키고 싶습니다."라는 의미는 무엇입니까? ? 문자열 "one", "two", "2"및 "four"만 일치 시키시겠습니까? –

+0

단어 나 그림으로 작성된 숫자를 캡처하고 싶습니다. 한 줄에 하나의 번호가 있습니다. – DJJ

답변

5

설명

번호를 제공하는 문자열의 번호와 일치한다이 정규 표현식은 공백이나 기호에 의해 둘러싸여있다.

(?<=\s|^)(?:[0-9]+|one|two|three|four|five|six|seven|eight|nine|ten)(?=\s|$)

enter image description here

라이브 예 : http://www.rubular.com/r/6ua7fTb8IS

하나의 외부 숫자의 철자가 단어 버전 포함하려면 - 10 당신이 사람들을 포함해야합니다. 이 정규식은 백에 0에서 숫자를 캡처 [드러내는 오타]

(?<=\s|^)(?:[0-9]+|(?:(?:twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety)\s)?(?:one(?:[\s-]hundred)?|two|three|four|five|six|seven|eight|nine)|ten|eleven|twelve|(?:thir|four|fif|six|seven|eight|nine)teen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|zero)(?=\s|$)

enter image description here

라이브 예 : http://www.rubular.com/r/EIa18nx731

펄 예

$string = <<END; 
... 1 child ... 
... three children ... 
... four children ...  
... 2 children... 
... five children 
END 
@matches = $string =~ m/(?<=\s|^)[0-9]+|one|two|three|four|five|six|seven|eight|nine|ten(?=\s|$)/gi; 
    print join("\n", @matches); 

를 산출
1 
three 
four 
2 
five 
+0

그 그래픽을 제작하기 위해 어떤 프로그램을 사용하고 있습니까 !! 놀랍습니다. –

+2

@ Rob Penridge. 감사합니다 :) debuggex.com을 사용하고 있습니다. 그것은 lookbehinds 또는 원자 그룹을 지원하지 않지만 표현 흐름을 이해하는 데는 여전히 편리합니다. regexper.com도 있습니다. 그들은 꽤 좋은 직업도하지만, 당신이 타이핑하는 것처럼 그것은 실시간이 아닙니다. –

+1

또한 나는 '90'이 'e'로 표기되었다고 생각합니다. 당신은 그것을 놓치고있는 것처럼 보입니다. http://www.thefreedictionary.com/ninety –