我有一个这样的文本列表:
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'"
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'"
'DATA - SOM THIN Escalation'
'DATA - SOM THIN HEARTBEAT Escalation'
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'"
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'"
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'"
我想从''
中提取字符串的""
部分。
做这个的最好方式是什么? '
和"
有什么区别?
如您所见,一些原始文本显示为'
,另一些具有"
包装。
答案 0 :(得分:0)
我有一些可行的方法,欢迎改进:
for rule in rules:
print(rule)
name = re.findall("'(.*)'", rule)
if name:
print(name[0])
else:
print(rule)
输出看起来像这样: (第一行输入,第二行输出。)
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'"
'DATA NM Escalation'
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'"
'DATA NM Escalation'
"DATA - Critical/Major DATA DATA Alerts to 'DATA NM Escalation'"
'DATA NM Escalation'
"DATA - Critical/Major DATA DATA Alerts to 'DATA NM Escalation'"
'DATA NM Escalation'
'DATA - SOM THIN Escalation'
'DATA - SOM THIN Escalation'
'DATA - SOM THIN HEARTBEAT Escalation'
'DATA - SOM THIN HEARTBEAT Escalation'
"DATA - Critical/Major DATA DATA Alerts to 'DATA NM Escalation'"
'DATA NM Escalation'
"DATA - Critical/Major DATA DATA Alerts to 'DATA NM Escalation'"
'DATA NM Escalation'
答案 1 :(得分:0)
没有RegEx:
//A struct for temporary holding of actors (and transforms) of actors that we hit
//that don't have an ASC. Used for environment impact GameplayCues.
struct FNonAbilityTarget
{
FGameplayTagContainer CueContainer;
TWeakObjectPtr<AActor> TargetActor;
FHitResult TargetHitResult;
bool bHasHitResult;
public:
FNonAbilityTarget()
: CueContainer(FGameplayTagContainer())
, TargetActor(nullptr)
, TargetHitResult(FHitResult(ENoInit::NoInit))
, bHasHitResult(false)
{
}
// (…)
输出:
lst = ["DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'",
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'",
'DATA - SOM THIN Escalation',
'DATA - SOM THIN HEARTBEAT Escalation',
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'",
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'",
"DATA - Critical/Major DATA Alerts to 'DATA NM Escalation'"]
for element in lst:
start_idx = element.find('\'')
if start_idx == -1:
start_idx = 0
end_idx = element.find('\'', start_idx+1, len(element))
if end_idx == -1:
end_idx = len(element)
print(element[start_idx:end_idx])
else:
print(element[start_idx+1:end_idx])