簡體   English   中英

更換圖案

[英]Replacing patterns

我正在嘗試構建一個涵蓋所有這些規則的正則表達式,但是我遇到了麻煩。 有人可以幫我嗎? 我是用蠻力做的,但是很慢。 規則如下:

如果詞組開頭的Camel大小寫( Camel case )或小寫( lower case )中有一些單詞鏈,而后沒有一個:字符,並且在單詞鏈的下方和上方都有空行,然后它將單詞的鏈轉換為CAPSLOCK后跟:字符

如果在短語“ NOT”的開頭的“ CAPSLOCK”中有一些單詞鏈,后跟一個“:”字符,並且在下一行中是文本的延續,也就是說,在開頭沒有其他單詞鏈行后跟一個的:字符,那么Concat的一個:性格和后右側的文本:字符。

否則,如果在CAPSLOCK中的短語NOT的開頭有一些單詞鏈,其后跟有':'字符,而下一行則在該行的開頭有另一個單詞鏈,其后跟有:字符,只需刪除鏈接並用空格替換。

同樣,也將所有\\n * * * \\n替換為一個空格,並將所有(Medical Transcription Sample Report)也替換為一個空格

一個文件示例:樣本類型/醫學專長:語音-語言
樣品名稱:言語治療-出院總結-2

DESCRIPTION:The patient was referred for outpatient skilled speech therapy, secondary to right hemisphere disorder, status post stroke. The patient attended nine outpatient skilled speech therapy sessions.  
(Medical Transcription Sample Report)  

* * *

The patient is a 69-year-old female, who was referred for outpatient skilled
speech therapy, secondary to right hemisphere disorder, status post stroke.
The patient attended nine outpatient skilled speech therapy sessions from her
initial evaluation on 12/01/08 to her last session on 01/09/09.  

The patient made some progress during therapy. She accomplished two and a half
out of her five short-term therapy goals. We did complete an oral mechanism
examination and clinical swallow evaluation, which showed her swallowing to be
within functional limits. The patient improved on her turn taking skills
during conversation, and she was able to listen to a narrative and recall the
main idea plus five details after a three-minute delay independently. The
patient continues to have difficulty with visual scanning in cancellation
task, secondary to her significant left neglect. She also did not accomplish
her sustained attention goal, which required her to complete tasks greater
than 80% accuracy for at least 15 minutes independently. Thus she also
continued to have difficulty with reading, comprehension, secondary to the
significance of her left neglect. The patient was initially authorized for 12
outpatient speech therapy sessions, but once again she only attended 9. Her
last session occurred on 01/09/09. She has not made any additional followup
sessions with me for over three weeks, so she is discharged from my services
at this time.  

KEYWORDS:speech - language, outpatient speech therapy, swallow evaluation, swallowing, skilled speech therapy, hemisphere disorder, speech therapy, speech

將會 :

 SAMPLE TYPE / MEDICAL SPECIALTY:Speech - Language  
SAMPLE NAME: Speech Therapy - Discharge Summary - 2 

    DESCRIPTION:The patient was referred for outpatient skilled speech therapy, secondary to right hemisphere disorder, status post stroke. The patient attended nine outpatient skilled speech therapy sessions.    The patient is a 69-year-old female, who was referred for outpatient skilled speech therapy, secondary to right hemisphere disorder, status post stroke. The patient attended nine outpatient skilled speech therapy sessions from her  initial evaluation on 12/01/08 to her last session on 01/09/09. The patient made some progress during therapy. She accomplished two and a half out of her five short-term therapy goals. We did complete an oral mechanism examination and clinical swallow evaluation, which showed her swallowing to be   within functional limits. The patient improved on her turn taking skills
    during conversation, and she was able to listen to a narrative and recall the main idea plus five details after a three-minute delay independently. The patient continues to have difficulty with visual scanning in cancellation task, secondary to her significant left neglect. She also did not accomplish her sustained attention goal, which required her to complete tasks greater than 80% accuracy for at least 15 minutes independently. Thus she also continued to have difficulty with reading, comprehension, secondary to the significance of her left neglect. The patient was initially authorized for 12
    outpatient speech therapy sessions, but once again she only attended 9. Her last session occurred on 01/09/09. She has not made any additional followup sessions with me for over three weeks, so she is discharged from my services at this time.  

    KEYWORDS:speech - language, outpatient speech therapy, swallow evaluation, swallowing, skilled speech therapy, hemisphere disorder, speech therapy, speech

另一個例子:

SAMPLE TYPE / MEDICAL SPECIALTY:Surgery  
SAMPLE NAME: Tonsillectomy & Adenoidectomy - 3 

DESCRIPTION:Tonsillectomy and adenoidectomy. Obstructive adenotonsillar hypertrophy with chronic recurrent pharyngitis.  
(Medical Transcription Sample Report)  

* * *

PREOPERATIVE DIAGNOSIS: Obstructive adenotonsillar hypertrophy with chronic recurrent pharyngitis.  

POSTOPERATIVE DIAGNOSIS:Obstructive adenotonsillar hypertrophy with chronic recurrent pharyngitis.  

SURGICAL PROCEDURE PERFORMED:Tonsillectomy and adenoidectomy.  

ANESTHESIA:General endotracheal technique.  

SURGICAL FINDINGS: A 4+/4+ cryptic and hypertrophic tonsils with 2+/3+ hypertrophic adenoid pads.  

INDICATIONS:We were requested to evaluate the patient for complaints of enlarged tonsils, which cause difficulty swallowing, recurrent pharyngitis, and sleep-induced respiratory disturbance. She was evaluated and scheduled for an elective procedure.  

DESCRIPTION OF SURGERY: The patient was brought to the operative suite and placed supine on the operating room table. General anesthetic was administered. Once appropriate anesthetic findings were achieved, the patient was intubated and prepped and draped in the usual sterile manner for a tonsillectomy. He was placed in semi-Rose ___ position and a Crowe Davis-type mouth gag was introduced into the oropharynx. Under an operating headlight, the oropharynx was clearly visualized. The right tonsil was grasped with the fossa triangularis and using electrocautery enucleation technique, was removed from its fossa. This followed placing the patient in a suspension position using a McIvor-type mouth gag and a red rubber Robinson catheter via the right naris. Once the right tonsil was removed, the left tonsil was removed in a similar manner, once again using a needle point Bovie dissection at 20 watts. With the tonsils removed, it was possible to visualize the adenoid pads. The oropharynx was irrigated and the adenoid pad evaluated with an indirect mirror technique. The adenoid pad was greater than 2+/4 and hypertrophic. It was removed with successive passes of electrocautery suction. The tonsillar fossa was then once again hemostased with suction cautery, injected with 0.5% ropivacaine with 1:100,000 adrenal solution and then closed with 2-0 Monocryl on an SH needle. The redundant soft tissue of the uvula was removed posteriorly and cauterized with electrocautery to prevent swelling of the uvula in the postoperative period. The patient's oropharynx and nasopharynx were irrigated with copious amounts of normal saline contained with small amount of iodine, and she was recovered from her general endotracheal anesthetic. She was extubated and left the operating room in good condition to the postoperative recovery room area.  

Estimated blood loss was minimal. There were no complications. Specimens
produced were right and left tonsils. The adenoid pad was ablated with
electrocautery.  

KEYWORDS:surgery, obstructive adenotonsillar hypertrophy, pharyngitis, tonsillectomy, adenoidectomy, uvula, obstructive, adenotonsillar, hypertrophy, hypertrophic, fossa, tonsils, oropharynx, electrocautery, pads

將會:

SAMPLE TYPE / MEDICAL SPECIALTY:Surgery  
SAMPLE NAME: Tonsillectomy & Adenoidectomy - 3 

DESCRIPTION:Tonsillectomy and adenoidectomy. Obstructive adenotonsillar hypertrophy with chronic recurrent pharyngitis.  

PREOPERATIVE DIAGNOSIS: Obstructive adenotonsillar hypertrophy with chronic recurrent pharyngitis.  

POSTOPERATIVE DIAGNOSIS:Obstructive adenotonsillar hypertrophy with chronic recurrent pharyngitis.  

SURGICAL PROCEDURE PERFORMED:Tonsillectomy and adenoidectomy.  

ANESTHESIA:General endotracheal technique.  

SURGICAL FINDINGS: A 4+/4+ cryptic and hypertrophic tonsils with 2+/3+ hypertrophic adenoid pads.  

INDICATIONS:We were requested to evaluate the patient for complaints of enlarged tonsils, which cause difficulty swallowing, recurrent pharyngitis, and sleep-induced respiratory disturbance. She was evaluated and scheduled for an elective procedure.  

DESCRIPTION OF SURGERY: The patient was brought to the operative suite and placed supine on the operating room table. General anesthetic was administered. Once appropriate anesthetic findings were achieved, the patient was intubated and prepped and draped in the usual sterile manner for a tonsillectomy. He was placed in semi-Rose ___ position and a Crowe Davis-type mouth gag was introduced into the oropharynx. Under an operating headlight, the oropharynx was clearly visualized. The right tonsil was grasped with the fossa triangularis and using electrocautery enucleation technique, was removed from its fossa. This followed placing the patient in a suspension position using a McIvor-type mouth gag and a red rubber Robinson catheter via the right naris. Once the right tonsil was removed, the left tonsil was removed in a similar manner, once again using a needle point Bovie dissection at 20 watts. With the tonsils removed, it was possible to visualize the adenoid pads. The oropharynx was irrigated and the adenoid pad evaluated with an indirect mirror technique. The adenoid pad was greater than 2+/4 and hypertrophic. It was removed with successive passes of electrocautery suction. The tonsillar fossa was then once again hemostased with suction cautery, injected with 0.5% ropivacaine with 1:100,000 adrenal solution and then closed with 2-0 Monocryl on an SH needle. The redundant soft tissue of the uvula was removed posteriorly and cauterized with electrocautery to prevent swelling of the uvula in the postoperative period. The patient's oropharynx and nasopharynx were irrigated with copious amounts of normal saline contained with small amount of iodine, and she was recovered from her general endotracheal anesthetic. She was extubated and left the operating room in good condition to the postoperative recovery room area. Estimated blood loss was minimal. There were no complications. Specimens produced were right and left tonsils. The adenoid pad was ablated with electrocautery.  

KEYWORDS:surgery, obstructive adenotonsillar hypertrophy, pharyngitis, tonsillectomy, adenoidectomy, uvula, obstructive, adenotonsillar, hypertrophy, hypertrophic, fossa, tonsils, oropharynx, electrocautery, pads

第三例:

  SAMPLE TYPE / MEDICAL SPECIALTY:Urology  
SAMPLE NAME: Laser Vaporization of Prostate 

DESCRIPTION:Cystopyelogram and laser vaporization of the prostate.  
(Medical Transcription Sample Report)  

* * *

PREOPERATIVE DIAGNOSIS:Benign prostatic hypertrophy.  

Postoperative Diagnosis:Benign prostatic hypertrophy.  

SURGERY: Cystopyelogram and laser vaporization of the prostate.  
amenesia:Spinal.  

ESTIMATED BLOOD LOSS:Minimal.  

FLUIDS:Crystalloid.  

BRIEF HISTORY:The patient is a 67-year-old male with a history of TURP, presented to us with urgency, frequency, and dribbling. The patient was started on alpha-blockers with some help, but had nocturia q.1h. The patient was given anticholinergics with minimal to no help. The patient had a cystoscopy done, which showed enlargement of the left lateral lobes of the prostate. At this point, options were discussed such as watchful waiting and laser vaporization to open up the prostate to get a better stream. Continuation of alpha-blockers and adding another anti-cholinergic at night to prevent bladder overactivity were discussed. The patient was told that his symptoms may be related to the mild-to-moderate trabeculation in the bladder, which can cause poor compliance.  

The patient understood and wanted to proceed with laser vaporization to see if
it would help improve his stream, which in turn might help improve emptying of
the bladder and might help his overactivity of the bladder. The patient was
told that he may need anticholinergics. There could be increased risk of
incontinence, stricture, erectile dysfunction, other complications and the
consent was obtained.  

PROCEDURE IN DETAIL: The patient was brought to the OR and anesthesia was applied. The patient was placed in dorsal lithotomy position. The patient was given preoperative antibiotics. The patient was prepped and draped in the usual sterile fashion. A #23-French scope was inserted inside the urethra into the bladder under direct vision. Bilateral pyelograms were normal. The rest of the bladder appeared normal except for some moderate trabeculations throughout the bladder. There was enlargement of the lateral lobes of the prostate. The old TUR scar was visualized right at the bladder neck. Using diode side-firing fiber, the lateral lobes were taken down. The verumontanum, the external sphincter, and the ureteral openings were all intact at the end of the procedure. Pictures were taken and were shown to the family. At the end of the procedure, there was good hemostasis. A total of about 15 to 20 minutes of lasering time was used. A #22 3-way catheter was placed. At the end of the procedure, the patient was brought to recovery in stable condition. Plan was for removal of the Foley catheter in 48 hours and continuation of use of anticholinergics at night.   

KEYWORDS:urology, laser vaporization of the prostate, cystopyelogram, benign prostatic hypertroph, benign prostatic hypertrophy, alpha blockers, laser vaporization, anticholinergics, laser, vaporization, prostate, bladder

將會:

SAMPLE TYPE / MEDICAL SPECIALTY:Urology  
SAMPLE NAME: Laser Vaporization of Prostate 

DESCRIPTION:Cystopyelogram and laser vaporization of the prostate.  

PREOPERATIVE DIAGNOSIS:Benign prostatic hypertrophy.  

POSTOPERATIVE DIAGNOSIS:Benign prostatic hypertrophy.  

SURGERY: Cystopyelogram and laser vaporization of the prostate.  

ANESTHESIA:Spinal.  

ESTIMATED BLOOD LOSS:Minimal.  

FLUIDS:Crystalloid.  


BRIEF HISTORY:The patient is a 67-year-old male with a history of TURP, presented to us with urgency, frequency, and dribbling. The patient was started on alpha-blockers with some help, but had nocturia q.1h. The patient was given anticholinergics with minimal to no help. The patient had a cystoscopy done, which showed enlargement of the left lateral lobes of the prostate. At this point, options were discussed such as watchful waiting and laser vaporization to open up the prostate to get a better stream. Continuation of alpha-blockers and adding another anti-cholinergic at night to prevent bladder overactivity were discussed. The patient was told that his symptoms may be related to the mild-to-moderate trabeculation in the bladder, which can cause poor compliance. The patient understood and wanted to proceed with laser vaporization to see if it would help improve his stream, which in turn might help improve emptying of the bladder and might help his overactivity of the bladder. The patient was told that he may need anticholinergics. There could be increased risk of incontinence, stricture, erectile dysfunction, other complications and the consent was obtained.   

PROCEDURE IN DETAIL: The patient was brought to the OR and anesthesia was applied. The patient was placed in dorsal lithotomy position. The patient was given preoperative antibiotics. The patient was prepped and draped in the usual sterile fashion. A #23-French scope was inserted inside the urethra into the bladder under direct vision. Bilateral pyelograms were normal. The rest of the bladder appeared normal except for some moderate trabeculations throughout the bladder. There was enlargement of the lateral lobes of the prostate. The old TUR scar was visualized right at the bladder neck. Using diode side-firing fiber, the lateral lobes were taken down. The verumontanum, the external sphincter, and the ureteral openings were all intact at the end of the procedure. Pictures were taken and were shown to the family. At the end of the procedure, there was good hemostasis. A total of about 15 to 20 minutes of lasering time was used. A #22 3-way catheter was placed. At the end of the procedure, the patient was brought to recovery in stable condition. Plan was for removal of the Foley catheter in 48 hours and continuation of use of anticholinergics at night.   

KEYWORDS:urology, laser vaporization of the prostate, cystopyelogram, benign prostatic hypertroph, benign prostatic hypertrophy, alpha blockers, laser vaporization, anticholinergics, laser, vaporization, prostate, bladder

使用1個以上的正則表達式替換更容易實現。

示例代碼:

import re

test_str = ("SAMPLE TYPE / MEDICAL SPECIALTY:Urology  \n"
    "SAMPLE NAME: Laser Vaporization of Prostate \n\n"
    "DESCRIPTION:Cystopyelogram and laser vaporization of the prostate. \n"
    "(Medical Transcription Sample Report)  \n\n"
    "* * *\n\n"
    "Brief History: The patient is a 67-year-old male with a history of TURP, presented to us with urgency, frequency, and dribbling. \n\n"
    "The patient was started on alpha-blockers with some help, but had nocturia q.1h. The patient was given anticholinergics with minimal to no help. \n\n"
    "KEYWORDS:urology, laser vaporization of the prostate, cystopyelogram, benign prostatic hypertroph, benign prostatic hypertrophy, alpha blockers, laser vaporization, anticholinergics, laser, vaporization, prostate, bladder")

regex_selective_replace_newlines = r"\s*?\r?\n(?![A-Z].*?:)(?:[^\w\s].*)?"

result = re.sub(regex_selective_replace_newlines, ' ', test_str, 0)

regex_extra_newlines = r"(\r?\n)([A-Z].*?:)"

def subst_extra_upper(m):
  return m.group(1) + m.group(1) + m.group(2).upper()

result = re.sub(regex_extra_newlines, subst_extra_upper, result, 0)

regex_many_spaces = r"[ ]{3,}"
result = re.sub(regex_many_spaces, "  ", result, 0)

if result:
    print (result)

輸出:

SAMPLE TYPE / MEDICAL SPECIALTY:Urology  

SAMPLE NAME: Laser Vaporization of Prostate 

DESCRIPTION:Cystopyelogram and laser vaporization of the prostate.  

BRIEF HISTORY: The patient is a 67-year-old male with a history of TURP, presented to us with urgency, frequency, and dribbling.  The patient was started on alpha-blockers with some help, but had nocturia q.1h. The patient was given anticholinergics with minimal to no help. 

KEYWORDS:urology, laser vaporization of the prostate, cystopyelogram, benign prostatic hypertroph, benign prostatic hypertrophy, alpha blockers, laser vaporization, anticholinergics, laser, vaporization, prostate, bladder

備注:

第一個刪除不需要的換行符。
還有不是以字母或空格開頭的行。

然后第二個正則表達式添加額外的換行符和上標題。

而第三個正則表達式僅用2個空格替換3個或更多空格。
使它看起來更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM