# Simple life, Complicated mind

## Wednesday, November 18, 2009

### regexp.c - Regular Expression Sample Example

regexp.c - Regular Expression Sample Example

```/*******************************************************
Copyright GPL Mike Chirico mmc mchirico@users.sourceforge.net
http://sourceforge.net/project/showfiles.php?group_id=79066

Note the possible combinations with '(1|2)(a|b)'.

The complete match '1b'
The match with '1'
The match with 'b'

./regexp '(1|2)(a|b)' 1b
match
2 0
\$0 = 1b, preg.re_nsub = 2
1 0
\$1 = 1, preg.re_nsub = 2
2 1
\$2 = b, preg.re_nsub = 2

./regexp '([a|c|e|g]{2}|[h-z])([0-9]|-)(a|b)' cc8b
match
4 0
\$0 = cc8b, preg.re_nsub = 3
2 0
\$1 = cc, preg.re_nsub = 3
3 2
\$2 = 8, preg.re_nsub = 3
4 3
\$3 = b, preg.re_nsub = 3

*******************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <regex.h>

#define NUM_MATCHES 4//max sub-matches

int
main (int argc, char *argv[])
{
regex_t preg;
regmatch_t pmatch[NUM_MATCHES];
size_t rm, i;
char pom[1024];

if (argc != 3)
{
printf ("Usage ./regexp [a-z]{2}[0-9]{2} a2b3ed23\n\n"
" this program is setup so that there will be a maximum\n"
" of 4 submatches\n"
"        ./regexp '([a|c|e|g]{2}|[h-z])([0-9]|-)(a|b)' cc8b \n"
"        match\n"
"        4 0\n"
"        \$0 = cc8b, preg.re_nsub = 3\n"
"        2 0\n"
"        \$1 = cc, preg.re_nsub = 3\n"
"        3 2\n"
"        \$2 = 8, preg.re_nsub = 3\n"
"        4 3\n"
"        \$3 = b, preg.re_nsub = 3\n");

return 1;
}

/* it's possible something won't compile like  ./regexp '*' abc */
if ((rm = regcomp (&preg, argv[1], REG_EXTENDED)) != 0)
{
fprintf(stderr,"Invalid expression:'%s'\n",argv[1]);
return 1;
}
(rm =
regexec (&preg, argv[2], NUM_MATCHES, pmatch,
0)) ? printf ("No match\n") : printf ("match\n");
for (i = 0; !rm && i <= preg.re_nsub; i++)
{
strncpy (pom, argv[2] + pmatch[i].rm_so,
pmatch[i].rm_eo - pmatch[i].rm_so);
printf ("%d %d\n", pmatch[i].rm_eo, pmatch[i].rm_so);
pom[pmatch[i].rm_eo - pmatch[i].rm_so] = '\0';
printf ("\$%d = %s, preg.re_nsub = %d\n", i, pom, preg.re_nsub);
}

regfree (&preg);

return 0;
}

```

Tutorials

Linux System Admin Tips: There are over 200 Linux tips and tricks in this article. That is over 100 pages covering everything from NTP, setting up 2 IP address on one NIC, sharing directories among several users, putting running jobs in the background, find out who is doing what on your system by examining open sockets and the ps command, how to watch a file, how to prevent even root from deleting a file, tape commands, setting up cron jobs, using rsync, using screen conveniently with emacs, how to kill every process for a user, security tips and a lot more. These tip grow weekly. The above link will download the text version for easy grep searching. There is also an html version here.

Breaking Firewalls with OpenSSH and PuTTY: If the system administrator deliberately filters out all traffic except port 22 (ssh), to a single server, it is very likely that you can still gain access other computers behind the firewall. This article shows how remote Linux and Windows users can gain access to firewalled samba, mail, and http servers. In essence, it shows how openSSH and Putty can be used as a VPN solution for your home or workplace.

MySQL Tips and Tricks: Find out who is doing what in MySQL and how to kill the process, create binary log files, connect, create and select with Perl and Java, remove duplicates in a table with the index command, rollback and how to apply, merging several tables into one, updating foreign keys, monitor port 3306 with the tcpdump command, creating a C API, complex selects, and much more.

Create a Live Linux CD - BusyBox and OpenSSH Included: These steps will show you how to create a functioning Linux system, with the latest 2.6 kernel compiled from source, and how to integrate the BusyBox utilities including the installation of DHCP. Plus, how to compile in the OpenSSH package on this CD based system. On system boot-up a filesystem will be created and the contents from the CD will be uncompressed and completely loaded into RAM -- the CD could be removed at this point for boot-up on a second computer. The remaining functioning system will have full ssh capabilities. You can take over any PC assuming, of course, you have configured the kernel with the appropriate drivers and the PC can boot from a CD. This tutorial steps you through the whole processes.

SQLite Tutorial : This article explores the power and simplicity of sqlite3, first by starting with common commands and triggers, then the attach statement with the union operation is introduced in a way that allows multiple tables, in separate databases, to be combined as one virtual table, without the overhead of copying or moving data. Next, the simple sign function and the amazingly powerful trick of using this function in SQL select statements to solve complex queries with a single pass through the data is demonstrated, after making a brief mathematical case for how the sign function defines the absolute value and IF conditions.

The Lemon Parser Tutorial: This article explains how to build grammars and programs using the lemon parser, which is faster than yacc. And, unlike yacc, it is thread safe.

How to Compile the 2.6 kernel for Red Hat 9 and 8.0 and get Fedora Updates: This is a step by step tutorial on how to compile the 2.6 kernel from source.

Virtual Filesystem: Building A Linux Filesystem From An Ordinary File. You can take a disk file, format it as ext2, ext3, or reiser filesystem and then mount it, just like a physical drive. Yes, it then possible to read and write files to this newly mounted device. You can also copy the complete filesystem, since it is just a file, to another computer. If security is an issue, read on. This article will show you how to encrypt the filesystem, and mount it with ACL (Access Control Lists), which give you rights beyond the traditional read (r) write (w) and execute (x) for the 3 user groups file, owner and other.

Working With Time: What? There are 61 seconds in a minute? We can go back in time? We still tell time by the sun?