473,655 Members | 3,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:1 5$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:1 3$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:1 1$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:1 7$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.redh at.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=recommende d, useposix=true, d_sigaction=def ine
usethreads=unde f use5005threads= undef useithreads=und ef
usemultiplicity =undef
useperlio=undef d_sfio=undef uselargefiles=u ndef usesocks=undef
use64bitint=und ef use64bitall=und ef uselongdouble=u ndef
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.9 6 20000731 (Red Hat Linux 7.2
2.96-109)', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=defi ne, longlongsize=8, d_longdbl=defin e,
longdblsize=12
ivtype='long', ivsize=4, nvtype='double' , nvsize=8, Off_t='off_t',
lseeksize=4
alignbytes=4, usemymalloc=n, prototype=defin e
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=fals e,
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 4415
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:1 5$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.n l>...
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:1 5$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
329
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 trouble with line breaks. I want to identify the start and end of blocks of text. Are there some tips someone can share? EG: in my text, I can grab a collection of everyones Phone number with:
3
2079
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 http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800 function fxnParseIt() { var sInputString = 'asp and database';
8
5578
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 variable is only alphanumeric (no strange characters). Here's the code: Regex regExp = new Regex("*");
6
4789
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 string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
2
6839
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 above) so I'm using this +\d+\d* however I want unique ones and not 3 copies of A15 as the above returns.
2
1946
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 valid IP Address", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Hand) txtIPAddress.Focus() Return End If
4
1847
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 values out that are separated by commas and the last value. I have no guarantee that there won't be a space before the comma (IE: "0 , 0") they could also be: "0,0" or "0, 0"
4
2633
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 match is never returned. The string that I give to the regex is one that contains the entire contents of a text file. I'm using the multi-line option and I've also tried stripping out the VbCr
6
9662
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 words "this is a .5k example" goes to "this is a 500 example" The replace should not touch '.5k' that occurs inside a word. For example:
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.