2013-04-21 48 views
0

我试图编写一个正则表达式来替换文本文件的某些区域之间的换行符,但仅限于某些关键字(开始关键字和结束关键字没有配对),但没有太多的运气通过第一部分。正则表达式只有当某个关键字对中的换行符被空格替换换行时

例输入:

QUESTION NO: 1130 

May a health plan require a provider to use a health care clearinghouse to conduct a HIPAA- covered transaction, or must the health plan acquire the ability to conduct the transaction directly with those providers capable of conducting direct transactions? 

A. A health plan may conduct its covered transactions through a clearinghouse, and may require a 
provider to conduct covered transactions with it through a clearinghouse. But the incremental cost of doing so must be borne by the health plan. It is a cost-benefit decision on the part of the health plan whether to acquire the ability to conduct HIPAA transactions directly with other entities, or to require use of a clearinghouse. B. A health plan may not conduct it's covered transactions through a clearinghouse 
C. A health plan may after taking specific permission from HIPAA authorities conduct its covered 
transactions through a clearinghouse D. is not as per HIPAA allowed to require provider to conduct covered transactions with it through 

a clearinghouse 

Answer: A 

Explanation: Personnel security always have to deal more with Operational controls, Operational 
controls provide the guidelines and the correct procedures to implement the different operations. Management controls are usually used only by managers. Human resources and Technical Controls are not related to personal security as the question states. See the different control definitions in your CISSP documentation. 

输出示例:

QUESTION NO: 1130 

May a health plan require a provider to use a health care clearinghouse to conduct a HIPAA- covered transaction, or must the health plan acquire the ability to conduct the transaction directly with those providers capable of conducting direct transactions? 

A. A health plan may conduct its covered transactions through a clearinghouse, and may require a provider to conduct covered transactions with it through a clearinghouse. But the incremental cost of doing so must be borne by the health plan. It is a cost-benefit decision on the part of the health plan whether to acquire the ability to conduct HIPAA transactions directly with other entities, or to require use of a clearinghouse. 
B. A health plan may not conduct it's covered transactions through a clearinghouse 
C. A health plan may after taking specific permission from HIPAA authorities conduct its covered transactions through a clearinghouse 
D. is not as per HIPAA allowed to require provider to conduct covered transactions with it through a clearinghouse 

Answer: A 

Explanation: A View is a display of one or more table shows that shows the table data. You can even retrieve part of the table and display the same to the user. Before a user is able to use a view, they must have both, permission on the view and all dependent objects. Views can also be used to implement security, for example you can create a view that only shows 3 of 5 columns contained in a table. Views are not used to provide integrity you can use constraints, rule or other components of database systems. 

我理想的情况是,"A. ""Answer: "之间的文本内容遭到移除换行和文本的其余部分保持不变。

+0

您使用什么语言? – HamZa 2013-04-21 14:18:10

+0

@HamZaDzCyber​​DeV:我个人使用Python/PHP/Javascript,但在RegexBuddy中的任何工作都可以。 – user1045217 2013-04-21 16:01:49

回答

0

在Python中,你可以这样做;

警告:这是不安全的依靠100%。如果X.发生在正常句子中,您将“拧紧”。

import re 

subject = '''A. A health plan may conduct its covered transactions through a clearinghouse, and may require a 
provider to conduct covered transactions with it through a clearinghouse. But the incremental cost of doing so must be borne by the health plan. It is a cost-benefit decision on the part of the health plan whether to acquire the ability to conduct HIPAA transactions directly with other entities, or to require use of a clearinghouse. B. A health plan may not conduct it's covered transactions through a clearinghouse 
C. A health plan may after taking specific permission from HIPAA authorities conduct its covered 
transactions through a clearinghouse D. is not as per HIPAA allowed to require provider to conduct covered transactions with it through''' 

result = re.findall(r'([A-Z]\.)(.*?)(?=[A-Z]\. |$)', subject, re.S) 

new_string = '' 

if len(result) > 0: 
    for entry in result: 
     new_string += entry[0] + entry[1].strip().replace('\n', ' ') + '\n' 
else: 
    print 'Nothing found' 

print new_string 

正前瞻查找无论是新X.或字符串的结尾。这可能是而不是工作寿命如果你匹配你的整个字符串。在这种情况下,让它继续寻找,直到找到Answer:和尾随的换行符((?=[A-Z]\. |\n+Answer: \w))。

端子输出

iMac2011:Desktop allendar$ python test.py 
A. A health plan may conduct its covered transactions through a clearinghouse, and may require a provider to conduct covered transactions with it through a clearinghouse. But the incremental cost of doing so must be borne by the health plan. It is a cost-benefit decision on the part of the health plan whether to acquire the ability to conduct HIPAA transactions directly with other entities, or to require use of a clearinghouse. 
B. A health plan may not conduct it's covered transactions through a clearinghouse 
C. A health plan may after taking specific permission from HIPAA authorities conduct its covered transactions through a clearinghouse 
D. is not as per HIPAA allowed to require provider to conduct covered transactions with it through 

更新代码

import re 

subject = '''QUESTION NO: 1130 

May a health plan require a provider to use a health care clearinghouse to conduct a HIPAA- covered transaction, or must the health plan acquire the ability to conduct the transaction directly with those providers capable of conducting direct transactions? 

A. A health plan may conduct its covered transactions through a clearinghouse, and may require a 
provider to conduct covered transactions with it through a clearinghouse. But the incremental cost of doing so must be borne by the health plan. It is a cost-benefit decision on the part of the health plan whether to acquire the ability to conduct HIPAA transactions directly with other entities, or to require use of a clearinghouse. B. A health plan may not conduct it's covered transactions through a clearinghouse 
C. A health plan may after taking specific permission from HIPAA authorities conduct its covered 
transactions through a clearinghouse D. is not as per HIPAA allowed to require provider to conduct covered transactions with it through 

a clearinghouse 

Answer: A 

Explanation: Personnel security always have to deal more with Operational controls, Operational 
controls provide the guidelines and the correct procedures to implement the different operations. Management controls are usually used only by managers. Human resources and Technical Controls are not related to personal security as the question states. See the different control definitions in your CISSP documentation.''' 

result = re.findall(r'([A-Z]\.)(.*?)(?=[A-Z]\. |\n{1,}Answer:\s\w+\s?\n+)', subject, re.S) 


new_string = '' 

if len(result) > 0: 
    for entry in result: 
     new_string += entry[0] + entry[1].strip().replace('\n\n', ' ').replace('\n', ' ') + '\n' 
else: 
    print 'Nothing found' 

print new_string 
+0

部分工作,但我在'解释'中有一些小故障。上面的示例文本被修改。 – user1045217 2013-04-21 23:22:00

+0

尝试更新的代码。我不是你“修改”的意思。你的意思是我自己编辑样本正则表达式('(?= [A-Z] \。| \ n {2,}回答| \ n +答案:\ w)')? – 2013-04-22 06:15:14