python - Possible to use caret and lookahead in set? -


i've read docs , looked on other questions haven't found answer.

is possible use lookahead within set, or have lookahead complement within set?

i want create set matches every character except dash preceded space. however, if there space not followed dash should match.

i thinking work, has not:

r'[^\s(?=\-)]' 

do lookaheads not work inside of set? if not, how go solving problem?

edited provide examples:

i have been trying find more accurate alternative to

r'([^\-]*)\-(.*)' 

which intended read line , separate artists titles.

applying re.match(r'([^\-]*)\-(.*)', "artist - title") should yield:

group(1) = "artist" group(2) = "title" 

however if artist name includes dash wrong parts of string captured.

example:

re.match(r'([^\-]*)\-(.*)', "jay-z - title") 

would yield:

group(1) = "jay" group(2) = "z - title" 

i want capture group capture spaces , dashes, not capture dash if preceded space (or space if followed dash, assuming lookahead vs lookbehind).

there 2 problems.

  1. a character class specifies number of possibilities match a single character in text being searched. lookahead , lookbehind assert conditions around character match, not part of character.

  2. the lookahead characters are not special in character class - treated literal characters. character class r'[^\s(?=\-)]' equivalent r'[^\-)(?\s=]' , means "match every character except =, ?, (, whitespace, , characters between \ , )".

for seem trying do, try matching every character except dash, , use alternation dashes not preceded space:

r'([^-]|(?<!\s-))' 

(edited after question added examples)

if can trust ' - ' separates artist song title, , on first occurrence, can use split method on each string, follows:

>>> "jay-z - title".split(' - ', 1) ['jay-z', 'title'] >>> 'prince - purple rain'.split(' - ', 1) ['prince', 'purple rain'] >>> 'prince - purple rain - love-song'.split(' - ', 1) ['prince', 'purple rain - love-song'] 

split takes substring on split, , optional maximum number of splits string. split returns source string split list of substrings on split argument, split argument removed.

specifying maximum number of splits n returns list of n+1 substrings first n instances of split target removed. subsequent instances of split target left in place.

split defaults left-to-right reading of string, , can right-to-left reading of string rsplit, supports maxsplit optional argument:

>>> 'prince - purple rain - love-song'.split(' - ', 1) ['prince', 'purple rain - love-song'] >>> 'prince - purple rain - love-song'.rsplit(' - ', 1) ['prince - purple rain', 'a love-song'] 

the built-in string type has lot of functionality, can find in python documentation.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -