Regex - Find line that contain certain words
When you are using text editor, such as Notepad++, which support regular expression search, to analyze log file, below are some regular expression that could be useful
... | ... Means boolean Or. Example, apple | pear match apple or pear
(...) Means grouping. Example, gr(a|e)y match gray or grey
? Means there is zero or one of the preceding element. Example, colo?ur match color or colour
* Means there is zero or more of the preceding element. Example, ab*c match ac, abc, abbc, etc..
+ Means there is one or more of the preceding element. Example, ab+c match abc, abbc, etc
^ Means match starting position of the line. Example, ^abc match abcxyz but not xyzabc
$ Means match ending position of the line. Example, abc$ match xyzabc but not abcxyz
. Means match any single character. Example, .at match cat, hat, lat, etc....
\b Means word boundary. It is typically used to avoid accidentally matching a word that appears inside some other word. For example, \bcat\b doesn't match catfish, but it matches cat regardless of what punctuation and whitespace surrounds it.
^hello&
The above match hello exactly. No character before or after.
2. Match zero or more occurrences of any character
hello.*world
The above match helloANYthinGworld as well as helloworld.
3. Find line containing a specific words.
^.*\b(Some Word)\b.*$
The above will search each line which contain Some Word
4. Find line containing any of the words
^.*\b(apple|orange|pears|durian)\b.*$
Basic
... | ... Means boolean Or. Example, apple | pear match apple or pear
(...) Means grouping. Example, gr(a|e)y match gray or grey
? Means there is zero or one of the preceding element. Example, colo?ur match color or colour
* Means there is zero or more of the preceding element. Example, ab*c match ac, abc, abbc, etc..
+ Means there is one or more of the preceding element. Example, ab+c match abc, abbc, etc
^ Means match starting position of the line. Example, ^abc match abcxyz but not xyzabc
$ Means match ending position of the line. Example, abc$ match xyzabc but not abcxyz
. Means match any single character. Example, .at match cat, hat, lat, etc....
\b Means word boundary. It is typically used to avoid accidentally matching a word that appears inside some other word. For example, \bcat\b doesn't match catfish, but it matches cat regardless of what punctuation and whitespace surrounds it.
Example
1. Match exact word in a line^hello&
The above match hello exactly. No character before or after.
2. Match zero or more occurrences of any character
hello.*world
The above match helloANYthinGworld as well as helloworld.
3. Find line containing a specific words.
^.*\b(Some Word)\b.*$
The above will search each line which contain Some Word
4. Find line containing any of the words
^.*\b(apple|orange|pears|durian)\b.*$
The above will search each line which contain apple or orange or pears or durian
Reference
1. http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm
Comments
Post a Comment