在python中解析大字符串

2024-05-16 21:07:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试解析一个字符串提取特定的单词。在

绳子

{{About|the ALGOL-like programming language|the scripting language formerly named Small|Pawn (scripting language)}}

'''SMALL''', Small Machine Algol Like Language, is a [[computer programming|programming]] [[programming language|language]] developed by Dr. [[Nevil Brownlee]] of [[Auckland University]].

==History==
The aim of the language was to enable people to write [[ALGOL]]-like code that ran on a small machine.  It also included the '''string''' type for easier text manipulation.

SMALL was used extensively from about 1980 to 1985 at [[Auckland University]] as a programming teaching aid, and for some internal projects.  Originally written to run on a [[Burroughs Corporation]] B6700 [[Main frame]] in [[Fortran]] IV, subsequently rewritten in SMALL and ported to a DEC [[PDP-10]] Architecture (on the [[Operating System]] [[TOPS-10]]) and IBM S360 Architecture (on the Operating System VM/[[Conversational Monitor System|CMS]]).

About 1985, SMALL had some [[Object-oriented programming|object-oriented]] features added to handle structures (that were missing from the early language), and to formalise file manipulation operations.

==See also==
*[[ALGOL]]
*[[Lua (programming language)]]
*[[Squirrel (programming language)]]

==References==
*[http://www.caida.org/home/seniorstaff/nevil.xml Nevil Brownlee]

[[Category:Algol programming language family]]
[[Category:Systems programming languages]]
[[Category:Procedural programming languages]]
[[Category:Object-oriented programming languages]]
[[Category:Programming languages created in the 1980s]] 


我想从SEE-ALSO部分提取ALGOL、Lua(编程语言)、Squirrel(编程语言)。(这些词不加括号或星号。)
我尝试过这些方法
字符串拆分,正则表达式。 我仍然不在, 感谢帮助。在



我使用的代码

^{pr2}$

字符串存储在


Tags: andtheto字符串inonsystemlanguage
2条回答

如果我理解正确,您需要==See also==和{}之间的字符,不包括{}。我将您的初始字符串命名为my_string。在

import re

# Sliced_string will only contain the characters between '==See also==' and '==References=='
sliced_string = re.findall(r'==See also==(.*?)==References==', my_string, re.DOTALL)[-1]

# Removes stars and brackets
for unwanted_char in '[]*':
    sliced_string = sliced_string.replace(unwanted_char, '')

# Creates a list of strings (also removes empty strings)
final_list = sliced_string.split('\n')
final_list = [elem for elem in final_list if elem != '']

print(final_list)

编辑:将字符串转换为列表。在

假设给定字符串中只有一个==See also====References==出现,代码就可以正常工作。在

print  re.findall(r"\*\[\[([^\]]*)\]\]",re.findall(r"==See also==((?:\s+\*\[\[(?:[^\]]*)\]\])+)",x)[0])

直接应用它并发送存储在x中的字符串

相关问题 更多 >