How to Regex append or prepend to lines in Notepad++ and Sublime Text

How to Regex append or prepend to lines in Notepad++ and Sublime Text

This is one of the most common tasks that come up when editing code. For both text editors the syntax is almost the same.

For both you would initiate the Search/Replace function and enable Regex for Search/Replace.

In the search field enter :

^(.+)$

This will select all lines individually in the file

In the replace field for Sublime Text enter:

cookie$1monster

This will prepend “cookie” to the beginning of each line and append monster to the end of each line

In the replace field for Notepad++ enter:

cookie\1monster

This will prepend “cookie” to the beginning of each line and append monster to the end of each line

That’s it. Some pretty pictures below to go along with the text madness.

Notepad++ Regex append prepend a line
Notepad++ Regex append prepend a line
Sublime Text Regex append prepend a line
Sublime Text Regex append prepend a line

Another way to check (assert) if text exists using Selenium Python

When using Selenium IDE to check if text exists in an element, regex is usually auto coded by the tool, something like this, when looking for the existence of the word Weather:

self.assertRegexpMatches(self.driver.find_element_by_xpath("html/body/div[1]/div[2]/div/div[1]/label").text, r"^[\s\S]*Weather[\s\S]*$")

Sometimes it is easier just to use “assert in” like this:

assert "Weather" in 
self.driver.find_element_by_css_selector("div.classname1.classname2>div.clearfix>label").text

change elements according to your situation

Python Selenium nosetest utf-8 issues resolved for non standard characters

Had some trouble recently testing with some non standard characters, mainly “ and ”

I originally had this line in the Python code:

self.assertEqual(self.driver.find_element_by_xpath("html/body/example").text,"and then comes “special text” in the UI", msg=None)

When running this script, an error would be returned:

SyntaxError: Non-ASCII character '\xe2' in file 

I had to add this to the top of the Python files

# coding=UTF-8

So now the Python (Selenium) script was running fine. However, when I tried to test with nose (nosetest) I got a different error:

AssertionError: u'and then comes \u201cspecial text\u201d in the UI' != 'and then comes \xe2\x80\x9cspecial text\xe2\x80\x9d in the UI'

soooo, then I added a decode at the end of the questionable string like this:

self.assertEqual(self.driver.find_element_by_xpath("html/body/example").text,"and then comes “special text” in the UI".decode('utf-8'), msg=None)

and it magically worked.

Save button grayed out (resolved) when setting proxy to manual on Android device (Nexus, LG Optimus)

I have not been able to find the resolution to this issue anywhere. Finally figured it out.

On multiple Android devices I was able to set the manual proxy simply by accessing the advanced network settings and saving the new values. However, on several Androids, the Save button was always grayed out.

All you need to do is populate the Wifi network password field as well. Then the Save button will not be grayed out. So each time you need to update these, first populate the password field, then change the proxy settings, IP, port etc. and it just might resolve your issue.

Save button grayed out (resolved) when setting proxy to manual on Android device (Nexus, LG Optimus)

Replace (Select) All except a string with Regex and Notepad++

This worked for me. I could not really find the full expression anywhere so here it is. It works with Notepad++ 6.1.5 . I know for a fact that it does not work with some other regular expression implementations.

Let’s say you have a bunch of text and you want Notepad++ to replace (select) all of the text except the string blahblah.
Open the Replace tool (Ctrl+H).
In the “Find what” field enter this:

(?<=blahblah)(.+)|(.+)(?=blahblah)|^((?!blahblah).)*$

Do not enter anything in the "Replace with" field if you want to remove everything except blahblah
Make sure that you also select the "Regular expression" radio button at the bottom of the Replace window.
Click on "Replace All" ...
Voila, you should have only the blahblah text left.

If you would like to remove everything except 5 digit numbers, you could do this:

(?<=[0-9]{5})(.+)|(.+)(?=[0-9]{5})|^((?![0-9]{5}).)*$

There is an explanation for this:

(?<=blahblah)(.+)

removes all characters after string blahblah to the end of the line (in lines that contain the blahblah string).

(.+)(?=blahblah)

removes all characters before string blahblah from the beginning of the line (in lines that contain the blahblah string).

^((?!blahblah).)*$

marks all lines that do not contain string blahblah

These are piped together with "|".

Is this the best way to do this? Most likely not, but it does get the job done and I did not see it documented elsewhere.

This post on stackoverflow.com definitely helped.