I want to know the following:
- How to remove the last few characters up to a point with regex? I want to remove all the last few characters up to the symbol
}. - Furthermore, how might I combine this with remove all the characters up to a point from the start? For example I have this:
'<!--//--><![CDATA[//><!--
jQuery.extend(Drupal.settings, {
"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"digital_pitt",
"theme_token":"f_ao6wF-W_eAlFz2xWPJsHm3K1MjZBC3Il-ncW6wxeY"
}, "urlIsAjaxTrusted":{"\/islandora\/search":true}});
//--><!]]>'
I can use the following to remove the first few characters up to {
^.*?,
However, I do not know how to reverse this and remove all the last few.
Here’s the expected outpuit:
{"basePath":"\/",
"pathPrefix":"",
"ajaxPageState":{
"theme":"digital_pitt",
"theme_token":"f_ao6wF-W_eAlFz2xWPJsHm3K1MjZBC3Il-ncW6wxeY"
}, "urlIsAjaxTrusted":{"\/islandora\/search":true}}
>Solution :
If you are using Python you can do that without using regex, using find() and rfind():
string = '''<!--//--><![CDATA[//><!--
jQuery.extend(Drupal.settings, {
"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"digital_pitt",
"theme_token":"f_ao6wF-W_eAlFz2xWPJsHm3K1MjZBC3Il-ncW6wxeY"
}, "urlIsAjaxTrusted":{"\/islandora\/search":true}});
//--><!]]>'''
print(string[string.find('{'):string.rfind('}')+1])
Output:
{
"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"digital_pitt",
"theme_token":"f_ao6wF-W_eAlFz2xWPJsHm3K1MjZBC3Il-ncW6wxeY"
}, "urlIsAjaxTrusted":{"\/islandora\/search":true}}