Python / Regex:如果一行包含某个特殊字符,则拆分字符串

时间:2018-02-23 02:19:34

标签: python regex regex-negation regex-lookarounds

我尝试在字符上拆分多行字符串,但前提是该行不包含:。遗憾的是,我无法在:字符)上看到使用Hello1 ( First : (), Second ) Hello2 ( First ) 使用负面回顾的简单方法,因为字符串中较早的另一行中['Hello1 (\nFirst : (),\nSecond', 'Hello2 (\nFirst \n'] 可能会发生FOR %A IN (*.gtf) DO gtf2dds.exe "%A"

例如,我想在<?php $query = $_POST['query']; $cSession = curl_init(); curl_setopt($cSession,CURLOPT_URL,"https://api.spiget.org/v2/search/resources/" + $query); curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true); curl_setopt($cSession,CURLOPT_HEADER, false); $result = curl_exec($cSession); curl_close($cSession); $json = json_decode($result); foreach ($json['items'] as $data) { echo "<h3>". $json['name']."</h3>"; echo "<h4>". $json['tag']."</h3>"; echo "\n"; echo "\n"; echo "\n"; };?> 上拆分以下字符串。

字符串:

#include<stdio.h>
#include<string.h>
int main(void){

char userInput[200];
int len;
int fingers[10] = {0,0,0,0,0,0,0,0,0,0 };
int i;
int total = 0;
int charCount = 0;
int strCount = 0;
int rightHand = 0;
int leftHand = 0;
int left = 0;
int right = 0;


printf("Enter some strings, end with cntrl-d: \n");
scanf("%s\n", userInput);
i = 0;
while( !feof(stdin) && userInput[i] != '\n' && userInput[i] != ' '){
len = strlen(userInput) - 1;

for(i = 0; i < len; i++){
if(userInput[i]=='q'||userInput[i]=='a'||userInput[i]=='z'||userInput[i]=='1'){
        fingers[0]++;
        total++;
        charCount++;
        left++;
    }
    else if(userInput[i]=='Q'||userInput[i]=='A'||userInput[i]=='Q'||userInput[i]=='!'){
        fingers[0]++;
        fingers[7]++;
        total = total + 2;
        charCount++;
        left++;
        right++;
    }
    else if(userInput[i]=='2'||userInput[i]=='w'||userInput[i]=='s'||userInput[i]=='x'){
            fingers[1]++;
            total++;
            charCount++;
            left++;
    }
    else if(userInput[i]=='@'||userInput[i]=='W'||userInput[i]=='S'||userInput[i]=='X'){
            fingers[1]++;
            fingers[7]++;
            total = total + 2;
            charCount++;
            left++;
            right++;
    }
    else if(userInput[i]=='3'||userInput[i]=='e'||userInput[i]=='d'||userInput[i]=='c'){
            fingers[2]++;
            total++;
            charCount++;
            left++;
    }
    else if(userInput[i]=='#'||userInput[i]=='E'||userInput[i]=='D'||userInput[i]=='C'){
            fingers[2]++;
            fingers[7]++;
            total = total + 2;
            charCount++;
            left++;
            right++;
    }
    else if(userInput[i]=='4'||userInput[i]=='r'||userInput[i]=='f'||userInput[i]=='v'
                ||userInput[i]=='5'||userInput[i]=='t'||userInput[i]=='g'||userInput[i]=='b'){
            fingers[3]++;
            total++;
            charCount++;
            left++;
    }
    else if(userInput[i]=='$'||userInput[i]=='R'||userInput[i]=='F'||userInput[i]=='V'
                ||userInput[i]=='%'||userInput[i]=='T'||userInput[i]=='G'||userInput[i]=='B'){
            fingers[3]++;
            fingers[7]++;
            total = total + 2;
            charCount++;
            left++;
            right++;
    }
    else if(userInput[i]=='6'||userInput[i]=='y'||userInput[i]=='h'||userInput[i]=='n'
                ||userInput[i]=='7'||userInput[i]=='u'||userInput[i]=='j'||userInput[i]=='m'){
            fingers[4]++;
            total++;
            charCount++;
            right++;
    }
    else if(userInput[i]=='^'||userInput[i]=='Y'||userInput[i]=='H'||userInput[i]=='N'
                ||userInput[i]=='&'||userInput[i]=='U'||userInput[i]=='J'||userInput[i]=='M'){
            fingers[4]++;
            fingers[0]++;
            total = total + 2;
            charCount++;
            right++;
            left++;
    }
    else if(userInput[i]=='8'||userInput[i]=='i'||userInput[i]=='k'||userInput[i]==','){
            fingers[5]++;
            total++;
            charCount++;
            right++;
    }
    else if(userInput[i]=='*'||userInput[i]=='I'||userInput[i]=='K'||userInput[i]=='<'){
            fingers[5]++;
            fingers[0]++;
            total = total + 2;
            charCount++;
            right++;
            left++;
    }
    else if(userInput[i]=='9'||userInput[i]=='o'||userInput[i]=='l'||userInput[i]=='.'){
            fingers[6]++;
            total++;
            charCount++;
            right++;
    }
    else if(userInput[i]=='('||userInput[i]=='O'||userInput[i]=='L'||userInput[i]=='>'){
            fingers[6]++;
            fingers[0]++;
            total = total + 2;
            charCount++;
            right++;
            left++;
    }
    else if(userInput[i]=='0'||userInput[i]=='p'||userInput[i]==';'||userInput[i]=='/'
                ||userInput[i]=='-'||userInput[i]=='='||userInput[i]=='['||userInput[i]==']'||userInput[i]=='\''){
            fingers[7]++;
            total++;
            charCount++;
            right++;
    }
    else if(userInput[i]=='*'||userInput[i]=='I'||userInput[i]=='K'||userInput[i]=='<'
                ||userInput[i]=='_'||userInput[i]=='+'||userInput[i]=='{'||userInput[i]=='}'||userInput[i]=='"'){
            fingers[7]++;
            fingers[0]++;
            total = total + 2;
            charCount++;
            right++;
            left++;
    }


}
if(left == 0){
    rightHand++;
}
else if(right == 0){
    leftHand++;
}
left = 0;
right = 0;


strCount++;
scanf("%s\n", userInput);

}

printf("%d strings entered, %d total characters\n", strCount, charCount);
printf(" %d typed only using left hand\n", leftHand);
printf(" %d typed only using right hand\n", rightHand);
printf("%d total keystrokes\n", total);
printf(" %d - left index\n", fingers[3]);
printf(" %d - left middle\n", fingers[2]);
printf(" %d - left ring\n", fingers[1]);
printf(" %d - left pinky\n", fingers[0]);
printf(" %d - right index\n", fingers[4]);
printf(" %d - right middle\n", fingers[5]);
printf(" %d - right ring\n", fingers[6]);
printf(" %d - right pinky\n", fingers[7]);


return 0;
}

输出:

{{1}}

1 个答案:

答案 0 :(得分:4)

可以使用Python,尽管不是&#34;开箱即用&#34;使用本机re模块。

第一种选择

较新的regex module支持可变长度的lookbehind,因此您可以使用

(?<=^[^:]+)\)
# pos. lookbehind making sure there's no : in that line

<小时/> 在Python

import regex as re

data = """
Hello1 (
First : (),
Second )

Hello2 (
First 
)"""

pattern = re.compile(r'(?<=^[^:]+)\)', re.MULTILINE)

parts = pattern.split(data)
print(parts)

哪个收益

['\nHello1 (\nFirst : (),\nSecond ', '\n\nHello2 (\nFirst \n', '']

<小时/>

第二种选择

或者,您可以匹配有问题的行,然后在(*SKIP)(*FAIL)之后让它们失败:

^[^:\n]*:.*(*SKIP)(*FAIL)|\)
# match lines with at least one : in it
# let them fail
# or match )

<小时/> 再次在Python

pattern2 = re.compile(r'^[^:\n]*:.*(*SKIP)(*FAIL)|\)', re.MULTILINE)
parts2 = pattern.split(data)
print(parts2)

a demo for the latter on regex101.com

<小时/>

第三种选择

好的,现在答案比以前想象的要长。您甚至可以在函数的帮助下使用本机re模块执行此操作。在这里,您需要先替换有问题的)并用替代品分开:

def replacer(match):
    if match.group(1) is not None:
        return "SUPERMAN"
    else:
        return match.group(0)

pattern3 = re.compile(r'^[^:\n]*:.*|(\))', re.MULTILINE)
data = pattern3.sub(replacer, data)
parts3 = data.split("SUPERMAN")
print(parts3)