解析 python 中的大字符串

Parsing large strings in 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(编程语言)。 (正是这些词不加括号或星号。)
我已经尝试过这些方法
字符串拆分,正则表达式。 我还是无处可去 帮助表示赞赏。



我使用的代码

import urllib.request,json,re

url = "http://en.wikipedia.org/w/api.php?format=json&action=query&titles=SMALL&prop=revisions&rvprop=content"
response = urllib.request.urlopen(url)
str_response = response.readall().decode('utf-8')
obj = json.loads(str_response)
a=str(obj['query']['pages']['1808130']['revisions'][0]['*'])
print(a)

字符串存储在a.

如果我没理解错的话,你需要 ==See also====References== 之间的字符,不包括那些 *[] 。我将您的初始字符串命名为 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 中的字符串。