Hi, I tried to port some python regex to JS, but the results were unexected
Which returns...Code:text = "[start 0.0]one one[end 5.25]this should not display[start 9]two two.[end 18.5][start 30]three three[end 50]"; document.write(text + "<br/ ><br/ >"); // define regex starts = /\[start\s([\d\.]+)\]/g; document.write(text.match(starts) + "<br/ ><br/ >"); words = /\[start\s[\d\.]+\](.*?)\[end\s[\d\.]+\]/g; document.write(text.match(words) + "<br/ ><br/ >"); ends = /\[end\s([\d\.]+)\]/g; document.write(text.match(ends) + "<br/ ><br/ >");
Here is the python, which returns what I expected...Code:[start 0.0]one one[end 5.25]this should not display[start 9]two two.[end 18.5][start 30]three three[end 50] [start 0.0],[start 9],[start 30] [start 0.0]one one[end 5.25],[start 9]two two.[end 18.5],[start 30]three three[end 50] [end 5.25],[end 18.5],[end 50]
returns...Code:s = '\[start\s([\d\.]+)\]' start = re.findall(s, text) # Start and end times for each word e = '\[end\s([\d\.]+)\]' end = re.findall(e, text) # this will get you the word you need to display. w = '\[start\s[\d\.]+\](.*?)\[end\s[\d\.]+\]' words = re.findall(w, text)
Anyone know why this is?Code:>>> start ['3.3', '8', '16'] >>> end ['6', '13', '20'] >>> words ['one one', 'two two.', 'three three']
Thanks.



Reply With Quote
Bookmarks