我有这个输入文本文件bio.txt
Enter for a chance to {win|earn|gain|obtain|succeed|acquire|get}
1⃣Click {Link|Url|Link up|Site|Web link} Below️
2⃣Enter Name
3⃣Do the submit(inside optin {put|have|positioned|set|placed|apply|insert|locate|situate|put|save|stick|know|keep} {shipping|delivery|shipment} adress)
需要定位这样的语法{win | earn | gain |获取|成功|获取|获取}并返回随机单词,例如:win
我如何在我的代码中从python中找到它:
input = open('bio.txt', 'r').read()
答案 0 :(得分:0)
您可以在每行上使用正则表达式搜索您的模式(根据您的示例private JPanel connectResource(){
JPanel remoteResource = new JPanel();
JPanel panelSouth = new JPanel();
//jstMpd is a Vector<Vector<String>>
jstMpd = getAlreadyMapped();
remoteResource.setLayout(new BorderLayout());
final JButton ResourceConnect = new JButton();
comboBox.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
Object lettereReadComboBox = comboBox.getSelectedItem();
final String letter = lettereReadComboBox.toString();
try {
alreadyMapped = ctrIfalreadyMapped(ResourceSelected, letter, typeOfConnection, jstMpd);
} catch (IOException e1) {
} catch (InterruptedException e1) {
}
if(alreadyMapped)
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Disconnect.png"));
else
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Drive-Network-Connected.png"));
ResourceConnect.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
try {
alreadyMapped = ctrIfalreadyMapped(ResourceSelected, letter, typeOfConnection, jstMpd);
} catch (IOException | InterruptedException e1) {
}
if(alreadyMapped){
try {
String localLetterMounted = deleteElement(ResourceSelected, letter, jstMpd);
if(localLetterMounted!=""){
new E_Mapped_Drives().unmountResource(localLetterMounted);
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Drive-Network-Connected.png"));
ResourceConnect.setActionCommand("!alreadyMapped");
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "You didn't selected any Letter of Remote Resource!\n Please Re-Try");
}
}
else{
String letterGetted = "";
String letterMounted= "";
if(typeOfConnection.equals("RDP") || typeOfConnection.equals("VNC")){
try {
letterGetted = letter;
letterMounted = new E_Mapped_Drives().mountResourceWindows(ResourceSelected, letterGetted, credential);
jstMpd.addElement(new Vector<String>(Arrays.asList(letterGetted, ResourceSelected,typeOfConnection,letterMounted)));
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Disconnect.png"));
} catch (IOException e) {
logger.warn("[BOTTONE-ResourceCONNECT][IOException] You didn't selected any letter!");
JOptionPane.showMessageDialog(null, "You didn't selected any Letter of Remote Resource!\n Please Re-Try");
}
}
else{
try {
letterGetted = new E_Mapped_Drives().mountResourceLinux(ResourceSelected, credential);//, comboBox.getSelectedItem(), credential);
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Disconnect.png"));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "You didn't selected any Letter of Remote Resource!\n Please Re-Try");
}
}
}
}
});
}
});
panelSouth.setBackground(getColorBackground());
remoteResource.add(comboBox, BorderLayout.NORTH);
remoteResource.add(ResourceConnect, BorderLayout.CENTER);
remoteResource.add(panelSouth, BorderLayout.SOUTH);
return remoteResource;
}
)。
然后,一旦找到它,只需根据您的示例将分隔符("\{.*\}"
)拆分。
最后随机返回列表中的一个元素。
正则表达式doc:https://docs.python.org/2/library/re.html
Python的字符串常用操作文档(包括"|"
)https://docs.python.org/2/library/string.html
答案 1 :(得分:0)
首先,您需要将文本文件读入字符串;找到模式&#34; {([a-z |] +)}&#34;使用正则表达式,将它们拆分为&#34; |&#34;将列表作为随机单词。它可以通过以下方式实现:
import re, random
seed = []
matches = re.findall('{([a-z|]+)}', open('bio.txt', 'r').read())
[seed.extend(i.split('|')) for i in matches]
input = random.choice(seed)