How to extract the substring between two markers?

Using regular expressions – documentation for further reference import re text=”gfgfdAAA1234ZZZuijjk” m = re.search(‘AAA(.+?)ZZZ’, text) if m: found = m.group(1) # found: 1234 or: import re text=”gfgfdAAA1234ZZZuijjk” try: found = re.search(‘AAA(.+?)ZZZ’, text).group(1) except AttributeError: # AAA, ZZZ not found in the original string found = ” # apply your error handling # found: 1234

Why does substring slicing with index out of range work?

You’re correct! ‘example'[3:4] and ‘example'[3] are fundamentally different, and slicing outside the bounds of a sequence (at least for built-ins) doesn’t cause an error. It might be surprising at first, but it makes sense when you think about it. Indexing returns a single item, but slicing returns a subsequence of items. So when you try … Read more

How to check whether a string contains a substring in JavaScript?

ECMAScript 6 introduced String.prototype.includes: const string = “foo”; const substring = “oo”; console.log(string.includes(substring)); // true includes doesn’t have Internet Explorer support, though. In ECMAScript 5 or older environments, use String.prototype.indexOf, which returns -1 when a substring cannot be found: var string = “foo”; var substring = “oo”; console.log(string.indexOf(substring) !== -1); // true