Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing the last few characters up to a point with regex

I want to know the following:

  1. 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 }.
  2. 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading