473,394 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Regex doesn't match - what am I doing wrong?

Hi,

I am having trouble matching a regex that combines a negated character
class and an anchor ($). Basically, I want to match all strings that
don't end in a digit. So I tried:

bash-2.05a@bermuda:15$perl -e 'while (<STDIN>) { if (/[^0-9]$/) {
print;}}'
skdsklds
skdsklds
sklskl2 <== why does this match? it ends in a digit.
sklskl2

This matched all strings regardless of whether or not they ended in a
digit. But the complemented regex seems to work fine:

bash-2.05a@bermuda:13$perl -e 'while (<STDIN>) { if (/[0-9]$/) {
print;}}'
sdkldsklds2
sdkldsklds2
sdsk2
sdsk2
sks <==== doesn't match as expected

I replaced [0-9] with [\d] but got the same results.

On the other hand, grep works as expected:

bash-2.05a@bermuda:9$grep '[0-9]$'
sdksdjk2
sdksdjk2
22221
22221
sdjkdsjk <== doesn't match as expected

bash-2.05a@bermuda:11$grep '[^0-9]$'
sdklsdklds
sdklsdklds
sdkldslk2 <== doesn't match as expected

What am I doing wrong? Here's the perl version info:

bash-2.05a@bermuda:17$perl -V
Summary of my perl5 (revision 5.0 version 6 subversion 1)
configuration:
Platform:
osname=linux, osvers=2.4.17-0.13smp, archname=i386-linux
uname='linux daffy.perf.redhat.com 2.4.17-0.13smp #1 smp fri feb 1
10:30:48 est 2002 i686 unknown '
config_args='-des -Doptimize=-O2 -march=i386 -mcpu=i686 -Dcc=gcc
-Dcf_by=Red Hat, Inc. -Dcccdlflags=-fPIC -Dinstallprefix=/usr
-Dprefix=/usr -Darchname=i386-linux -
Dvendorprefix=/usr -Dsiteprefix=/usr -Uusethreads -Uuseithreads
-Uuselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db -Di_ndbm -Di_gdbm
-Di_shadow -Di_syslog -Dman3ext=3pm
'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef
usemultiplicity=undef
useperlio=undef d_sfio=undef uselargefiles=undef usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
Compiler:
cc='gcc', ccflags ='-fno-strict-aliasing -I/usr/local/include',
optimize='-O2 -march=i386 -mcpu=i686',
cppflags='-fno-strict-aliasing -I/usr/local/include'
ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.2
2.96-109)', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=define, longlongsize=8, d_longdbl=define,
longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=4
alignbytes=4, usemymalloc=n, prototype=define
Linker and Libraries:
ld='gcc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lnsl -ldl -lm -lc -lcrypt -lutil
perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil
libc=/lib/libc-2.2.5.so, so=so, useshrplib=false,
libperl=libperl.a
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef,
ccdlflags='-rdynamic'
cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options:
Built under linux
Compiled at Apr 1 2002 12:23:22
@INC:
/usr/lib/perl5/5.6.1/i386-linux
/usr/lib/perl5/5.6.1
/usr/lib/perl5/site_perl/5.6.1/i386-linux
/usr/lib/perl5/site_perl/5.6.1
/usr/lib/perl5/site_perl/5.6.0
/usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.6.1/i386-linux
/usr/lib/perl5/vendor_perl/5.6.1
/usr/lib/perl5/vendor_perl
Jul 19 '05 #1
2 4398
On Wed, 10 Mar 2004 14:47:40 -0800, Sriram wrote:

The string 'sklskl2' does not end in a digit, it ends in a character
return. That's probably why everything always matches. If you chop the $_
first, like

perl -e 'while (<stdin>){chop $_; if (/[^0-9]$/) {print } }'

results will look more promising.

Hope this helps.

Hi,

I am having trouble matching a regex that combines a negated character
class and an anchor ($). Basically, I want to match all strings that
don't end in a digit. So I tried:

bash-2.05a@bermuda:15$perl -e 'while (<STDIN>) { if (/[^0-9]$/) {
print;}}'
skdsklds
skdsklds
sklskl2 <== why does this match? it ends in a digit.
sklskl2


Jul 19 '05 #2
Hi,

Thanks! That works much better; so does '/[^0-9]\n/' or !/[0-9]$/.

My Perl book says "The $ and \Z assertions can match not only at the
end of the string, but also one character earlier than that, if the
last character of the string happens to be a newline." This is also
how I'm used to regexs working in grep/sed etc.

I'm baffled that $ behaves differently when used in conjunction with a
negated character class. The complemented regex /[0-9]$/ works without
chop. Why the subtle difference?

Sriram

"Arno H.P. Reuser" <bi************@xs4all.nl> wrote in message news:<pa****************************@xs4all.nl>...
On Wed, 10 Mar 2004 14:47:40 -0800, Sriram wrote:

The string 'sklskl2' does not end in a digit, it ends in a character
return. That's probably why everything always matches. If you chop the $_
first, like

perl -e 'while (<stdin>){chop $_; if (/[^0-9]$/) {print } }'

results will look more promising.

Hope this helps.

Hi,

I am having trouble matching a regex that combines a negated character
class and an anchor ($). Basically, I want to match all strings that
don't end in a digit. So I tried:

bash-2.05a@bermuda:15$perl -e 'while (<STDIN>) { if (/[^0-9]$/) {
print;}}'
skdsklds
skdsklds
sklskl2 <== why does this match? it ends in a digit.
sklskl2

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Masahiro Ito | last post by:
I have attached a block of text similar to the type that I am working with. I have been learning a lot about Regex - it is quite impressive. I can easily capture bits of info, but I keep having...
3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
8
by: Bibe | last post by:
I've been trying to get this going for awhile now, and need help. I've done a regex object, and when I use IsMatch, it's behavior is quite weird. I am trying to use Regex to make sure that a...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
2
by: D | last post by:
My first attempt at this and I'm searching formulas like so RIGHT(TEXT(A15,'yy'),1)*1000+A15-CONCATENATE(1,'-','jan','-',TEXT(A15,'yy'))+1 I want to extract the row / col coordinates (A15 in...
2
by: Michael R. Pierotti | last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$") Dim m As Match = reg.Match(txtIPAddress.Text) If m.Success Then 'No need to do anything here Else MessageBox.Show("You need to enter a...
4
by: sklett | last post by:
given one of these two string: ProtocolRecord(0, 100, 100, arZeroMax , 1, 0, SKIP,0), ProtocolRecord(0, 100, 100, arSetSet , 4, 0, DJMPNZSTOP, (void *) &Protocol_TD) I want to pull all the...
4
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
6
by: Gary Bond | last post by:
Hi All, Being a bit of a newbie with regex, I am confused when using word boundaries. For instance, I want to replace all the stand alone '.5k' that occur in an input string, with 500. In other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.