Wednesday, January 7, 2015

Useful Regular Expressions

Often times we need to search and if needed, replace the text in the large document or extract logs based on some fixed string values.  This post lists some of the regular expressions I have used very often.  This post will be updated regularly as I come across new regular expressions for new use cases:

Use Case 1:

Find a position if the first occurrence of the string.  for example, find the first occurrence of the string "SF_".  This will highlight the line up to that string.

^.*?(?=SF_)

Use Case 2:

Select whole line.

(.+)

Use Case 3:

Surround the entire line with the double quotes and add "," after the endquote.

"\1",

Use Case 4:

Find end of the line and add "," at the end.

Search: ([^\n])$
Replace : \1,

Use Case 5:

Find a string and highlight up to the end of the line.

Search: <string>.*

Example:
query.*


That is all for now.  As I mentioned in the beginning, I will update this post as I come across interesting scenarios.