[Tutor] regular expression
arbaro arbaro
arbaro at gmail.com
Thu Aug 3 22:16:38 CEST 2006
Hello,
Im just answering my own email, since I just found out what my error was.
>From a regular expression howto: http://www.amk.ca/python/howto/regex/regex.html
The match() function only checks if the RE matches at the beginning of
the string while search() will scan forward through the string for a
match. It's important to keep this distinction in mind. Remember,
match() will only report a successful match which will start at 0; if
the match wouldn't start at zero, match() will not report it.
That was exactly my problem. Replacing r.match(line) for
r.search(line) solved it.
Sorry for having bothered you prematurely.
On 8/3/06, arbaro arbaro <arbaro at gmail.com> wrote:
>
> Hello,
>
> I'm trying to mount an usb device from python under linux.
> To do so, I read the kernel log /proc/kmsg and watch for something like:
> "<6> /dev/scsi/host3/bus0/target0/lun0/:<7>usb-storage: device scan complete"
>
> When I compile a regular expression like:
> "r = re.compile('<\d+>\s/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')"
> It is found. But I don't want the <\d+>\s or '<6> ' in front of the path, so I tried:
> "r = re.compile('/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')"
> But this way the usb device path it is not found.
>
> So what i'm trying to do is:
> - find the usb device path from the kernel log with the regular expression.
> - Determine the start and end positions of the match (and add /disc or /part1 to the match).
> - And use that to mount the usb stick on /mnt/usb -> mount -t auto match /mnt/usb
>
> If anyone can see what i'm doing wrong, please tell me, because I don't understand it anymore.
> Thanks.
>
> Below is the code:
>
> # \d+ = 1 or more digits
> # \s = an empty space
>
> import re
>
> def findusbdevice():
> ''' Returns path of usb device '''
> # I did a 'cat /proc/kmsg /log/kmsg' to be able to read the kernel message.
> # Somehow I can't read /proc/kmsg directly.
> kmsg = open('/log/kmsg', 'r')
> r = re.compile('/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')
> #r = re.compile('<\d+>\s/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')
> for line in kmsg:
> if 'usb-storage' in line and r.match(line):
> print 'Success', line
>
More information about the Tutor
mailing list