Matching numerical range with Regular Expression

Regular Expression is powerful for matching texts with alphabets but numerical ranges. For example, a to z can be matched by [a-z] but 1 to 22 can not be matched by [1-22]. Intuitively, we can numerically assign the range we want to match by (1|2|3|…|21|21|22). Numerical assignment is precisely but we need a more elegant and flexible way.

First, I’ve written the pattern, ([1-9]|1[1-9]|2[0-2]), but it fails to match 20. The reason is the first character of 20 is matched by the pattern [1-9] and the function exit. To fix this problem, the priority for matching should be altered and the pattern becomes: (1[0-9]|2[0-2]|[1-9]). It matches the texts from 1 to 22 successfully but the sub-string within 010 which is unexpectedly.

According to Matching Numeric Ranges with a Regular Expression, we can design the pattern with word boundary (\b). The final pattern is designed


where the texts (1, 2, 3, …, 20, 21, 22) can be matched except for sub-strings of the text (eg. 12345).

For online testing, I suggest It’s very useful. Enjoy it. 🙂

Install java-7-oracle with PPA on Ubuntu

This note is based on Howto- Install Oracle Java7 in Ubuntu12.04 Precise Pangolin | PPA. The download procedure in oracle-java7-installer package is broken. We should modify it for the rest automatically installation. The summary of my steps are listing:

1. Purge installed JDKs:
# apt-get purge openjdk* sun-java6*

2. Add repository for installation:

# add-apt-repository ppa:eugenesan/java
# apt-get update

3. Modify the installation procedure:
# vim /var/lib/dpkg/info/oracle-java7-installer.postinst
Comment out lines 103-106 which refers to download JDK. The contents may seems like:

WGETRC=wgetrc wget $PARTNER_URL \
|| fp_exit_with_error “download failed”
rm -f wgetrc

4. Manual download JDK source and move it into installation directory:
# mv {source} /var/cache/oracle-java7-installer/
The type (x86/x64) and name of the source can be found within the directory (if you had try to install). Eg: jdk-7u3-linux-x64.tar.gz.

5. Resume the installation:
# apt-get install oracle-java7-installer
All things done. Happy enjoy. 🙂

Pretty permalinks and search urls for WordPress on lighttpd

To make a blog Search Engine (SE) friendly, one feasible way is to make all requested URIs permanently. It is easier for Apache web service than lighttpd, because there are more resources and technical notes on the Net. This post is about how to set up the pretty permalinks and to introduce a partial solution for the unfriendly search URIs for WordPress 3.3 on lighttpd.

First, for pretty permalinks, the reference is Using Permalinks. I want to use the Day and Name format as an SEO friendly URI. Unfortunately, except for Apache’s rewrite rules, there always exist index.php as a bridge for processing. Then the format seems


. To eliminate the bridge string, we can still use Custom Structure and set up rewrite ules on lighttpd.

There are ways to write proper rewrite rules within lighttpd. Based on the references: lighttpd rewrite rule for wordpress 3 and other resources from Google (keywords: lighttpd, wordpress, permalinks), the final configuration is very simple. We only need to add

server.error-handler-404 = "/index.php"

into lighttpd. This rule indicate that the request will be passed into the index page when the page (or URI) is not exist physically. It is depend on the truth that almost all of the requests are handled by the index page (like the posts, pages, feeds).

Second, to fix the unfriendly search URIs, we can use plugins or to add a redirect rule into lighttpd. The search URIs are formatted as


and we want to replace it into


. It is SEO friendly and is VERY IMPORTANT when we use 404 handler as rewrite rule. For example, if there are more than one page for the search term T, the first result page’s URI is


where the second result page is


. Because the request of page 2 is not exist physically, the request will be passed into index.php and the search term (?s=T) will be ignored. The reason for why search will be ignored remains unknown.

To make the search and paged view work, we can set a redirect rule to fix it by adding

url.redirect = ( "^/\?s=(.*)" => "/search/$1" )

into configuration on lighttpd.

Finally, the URIs are all search engine friendly and work correctness. Enjoy it. 🙂

Script to check tunnel status periodically on Windows

This post is delayed for more than 10 months because I forgot about it. Sorry about that.

To improve the availability of the reverse-tunnel, we would check up the status periodically via Scheduled Tasks. Now, we need to write a batch script and add it into Windows.

First, the script I wrote:


SET _SERVER={Server ip}
SET _PORT={Server port}

SET _SERVICE={Your service name installed in Windows}

start /MIN /B netstat.exe -nav -p TCP | FIND /C "%_SERVER%:%_PORT%" | FIND "1" > nul 2>&1

start /MIN /B net stop %_SERVICE%
start /MIN /B net start %_SERVICE%

Actually, I’m a newbie of batch script in Windows and the script may be ugly. The script first defined some variables we will use later: the destination server’s ip and port, and the service name we installed in Windows.

The main function is TUNNEL_CHECK. We start a command without creating a new window (/B) or minimizing it (/MIN). For netstat.exe, we list all active connections numerically (-an) and filtered with TCP only (-p TCP). Then we count the lines (/C) matched to our server string by FIND. We will restart the service if there is error exist. Otherwise, exit the program.

Second, we shall add this script into Windows. Open the the configure window via: Start > All Programs > Accessories > System Tools > Scheduled Tasks. Then create a new task with the script. I’ll list the settings when editing the task.

In the Schedule tab of the task, we need two schedules. One is Run at system startup and another one is ran periodically: In my settings, the script is a Repeat task for every 1 hour and its duration is 23 hours. The tasks itself is Scheduled as Daily from 12:00 AM every 1 Day.

Generally speaking, this script will run at system start-up, and every hour of every day. To avoid any problem, I uncheck all boxes in the Settings tab. Another note is to set up the task ran as a valid user. This step will make it run at system start-up successfully. If the user is password-protected, we shall also set the password.

It’s all the details of my checking script. Enjoy it. 🙂

Suppress massively SNMP notices in message log

In my CentOS, with SNMP, it arise much more SNMP logs in message periodically. Like this:

Connection from UDP: []:xxxxx
Received SNMP packet(s) from UDP: []:xxxxx

Although the notices are harmless, it may drown out other useful messages. To suppress these messages, we can modify the parameters passed to SNMP daemon.

There is a default configuration in /etc/sysconfig/snmpd.options with a comment out option line:

# OPTIONS=”-Lsd -Lf /dev/null -p /var/run/ -a”

This line indicate SNMP’s log facility via syslog is for daemon (-Lsd) and it will log all levels (there are 7 levels). To suppress the notices, the configuration is modified as:

OPTIONS="-LS0-4d -Lf /dev/null -p /var/run/ -a"

. The parameter -LS is to specify the level we want to log, and we set level 0 to 4 (emergency, alert, critical, error, warning) for it. Whereas the level 5 is for notice, 6 for info and 7 for debug are not what we need.

Finally, the log is clearly as usual. 😛

Disable automount of GNOME

In GNOME Shell, automount is convenient while devices inserting into computer. But it is annoying for me because I’m booting from an USB stick. The rest partitions in USB will be mounted and popup a notification to ask what action should be taken.

To disable the auto-mounting behavior built-in Nautilus (the file manager of GNOME), we need to change the configurations via dconf-editor or gsettings with the path: automount

For example, to DISABLE the behavior via command-line:

$ gsettings set automount false

. Other wise, just set as true to enable it. Enjoy it. 🙂

The reference is: How to disable automount in nautilus’s preferences.

Set MySQL default character set as UTF8 on CentOS

The default character set of MySQL installed in CentOS 5 is latin while a feasible one for CJKs is UTF-8.

mysql> SHOW VARIABLES LIKE '%character%';
| Variable_name | Value |
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |

To enable whole system UTF-8, we need to add the following codes in /etc/my.cnf:
default-character-set = utf8

character-set-server = utf8

mysql> SHOW VARIABLES LIKE '%character%';
| Variable_name | Value |
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |

There are lots of additional settings in my Gentoo box, but it seems we don’t need right now. Maybe it can be use later. enjoyt it. 😉

[120302] The parameter default-character-set = utf8 for server configuration in 5.1.61 (or 5.1) is deprecated. We should use character-set-server = utf8 instead.