2016-07-30 2 views
-2

C가 Celsius 인 경우 특정 상황 (예 :)에서 대문자로 대문자를 사용하고 싶습니다. 지금까지 내 정규식 :Regex 대문자 특정 문자

((?!\s)[c](?=\s)|(?!\d)[c](?=\d)|[c](?=-)) 

예 텍스트 :

Some plastic insert c lids were cracked, temperature was between 8c and 8.8c. 

나는 숫자 후 모든 독방 C 년대와 C의의를 활용하고자합니다. 모든 포인터가 도움이 될 것입니다. 당신은 공간 또는 숫자 후 c을 바꾸려면 미리 부정적인 모습 후자 스탠드, 다음 뒤에 모양을 사용할 필요가있는 동안 당신은 (?<=...) 대신 (?!...), 뒤에 보기를위한 전자 스탠드가 필요

답변

0

제약 :

str1 = "Some plastic insert c lids were cracked, temperature was between 8c and 8.8c." 

import re 
re.sub(r"(?<=\s)c(?=\s)|(?<=\d)c", "C", str1) 
# 'Some plastic insert C lids were cracked, temperature was between 8C and 8.8C.' 

당신이 고독한 정의하는 방법에 따라 단어 경계가 더 적합 할 수 있습니다

re.sub(r"\bc\b|(?<=\d)c", "C", str1) 
# 'Some plastic insert C lids were cracked, temperature was between 8C and 8.8C.' 
주석의 경우에 대한

업데이트 :

str2 = "Ensure that noodles soaked in water are kept at or 4 c. Noodles are moved to walk in cooler. * Ensure that perishable food is chilled rapidly as * A) temperature from 60 c-20 C must fall within two hrs" 

re.sub(r"\bc\b|(?<=\d)c", "C", str2) 
# 'Ensure that noodles soaked in water are kept at or 4 C. Noodles are moved to walk in cooler. * Ensure that perishable food is chilled rapidly as * A) temperature from 60 C-20 C must fall within two hrs' 
+0

나는이 '더 나은'R 될 것이라고 생각 C (= \의 | $?) | ((<= [\ S \ D]?) ? <= ^) c (? = \ s) '' – rock321987

+0

@ rock321987 절대적으로, 그것은 독방에 대한 OP의 의미에 달려 있습니다. 더 일반적인 단어 경계 옵션을 추가했습니다. – Psidom

+0

나는 이것들을 시험해 보았고 몇몇 c 's는 놓쳤다. "물에 담근 국수는 4 ℃에 보관한다. 국수는 더 차갑게 걸을 때 옮긴다. * 부패하기 쉬운 음식이 빨리 식혀 지는지 확인한다. * A) 60에서의 온도 c-20 C는 2 시간 이내에 있어야합니다. " – MoreScratch