473,480 Members | 2,347 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Negative regular expression not working at end of string

nithinpes
410 Recognized Expert Contributor
I tried the following regular expression in script to extract lines not begining with capital letters and not ending with numbers.
Expand|Select|Wrap|Line Numbers
  1.  if ($str =~/^[^A-Z].+[^0-9]$/)       ## similarly /^[^A-Z].+\D$/
  2.    {
  3.     ######
  4.    }
  5.  
This didn't work. The pattern condition was applied only to begining of string. The lines returned contained both digits & non-digits at the end.

However, I could achieve the task as follows:
Expand|Select|Wrap|Line Numbers
  1.  if (($str !~ /^[A-Z]/)&&($str !~ /[0-9]$/))
  2.    {
  3.     ######
  4.    }
  5.  
But, I need to know why the previous expression didn't work. I understand the problem with using negative grammar for pattern matching. But, I am unable to find any conceptual/ logical error in the first expression.
To boil down the above query,
Expand|Select|Wrap|Line Numbers
  1. if(/^[^A-Z]/)
  2. { print $_; }
  3.  
will successfully return lines not begining with capital letters, while
Expand|Select|Wrap|Line Numbers
  1. if(/[^0-9]$/)
  2. { print $_; }
  3.  
fails to return lines not ending with digits.It returns all lines.

Any help would be greatly appreciated.
Jan 21 '08 #1
2 1759
KevinADC
4,059 Recognized Expert Specialist
The end of string anchor "$" is supposed to match the pattern before any newline at the end of the string/line. And it does in this example:

Expand|Select|Wrap|Line Numbers
  1. while(<DATA>){
  2.    print if ($_ !~ /[0-9]$/);
  3. }
  4.  
  5. __DATA__
  6. This is a test
  7. This is a test 96
  8. this is a test
  9. this is a test 99
  10. this is another test
The lines with digits on the end do not get printed. But when I change it to a negated character class the newline is not being ignored:

Expand|Select|Wrap|Line Numbers
  1. while(<DATA>){
  2.    print if ($_ =~ /[^0-9]$/);
  3. }
  4.  
  5. __DATA__
  6. This is a test
  7. This is a test 96
  8. this is a test
  9. this is a test 99
  10. this is another test 
and all the lines are printed. But if I chomp() each line the ones with digits on the end are not printed:

Expand|Select|Wrap|Line Numbers
  1. while(<DATA>){
  2.    chomp;
  3.    print if ($_ =~ /[^0-9]$/);
  4. }
  5.  
  6. __DATA__
  7. This is a test
  8. This is a test 96
  9. this is a test
  10. this is a test 99
  11. this is another test
Evidently a negated character class tied to the end of the line/string does not ignore the record seperator (a newline in this case) and the match fails. I could not find this documented anywhere though.
Jan 21 '08 #2
nithinpes
410 Recognized Expert Contributor
Thanks a lot!
This concept is not documented in any of the books that I refered or in online references.
Jan 21 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1230
by: brian fischer via .NET 247 | last post by:
Hello, I am trying to use a regular expression in Visual Basic Script tomatch some filenames within a folder. However, I am having someissues with the negative range character. I cannot seem to...
11
3856
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
3
1806
by: pagates | last post by:
Hi All, It might be because its the end of a long week, but I can't figure out the correct way to write a regular expression to find all matches except a specified string. For example, take...
7
3791
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
5128
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
2
7240
by: writebrent | last post by:
I think I need to do a negative lookahead with a regular expression, but I'm a bit confused how to make it all work. Take these example texts: Need to match these two: =========================...
7
3113
by: intrader | last post by:
The regular expression is /(?!((00000)|(11111)))/ in oRe. That is oRE=/(?!((00000)|(11111)))/ The test strings are 92708, 00000, 11111 in checkStr The expression used is checkStr.search(oRE). The...
6
8355
by: =?Utf-8?B?ZGlhdG9tQG5ld3Nncm91cC5ub3NwYW0=?= | last post by:
Hello, I have a data entry windows form. One of the text boxes allows the user to enter a string. I need this text box to only allow users to type in a negative integer value (e.g. -1, -2,...
4
1616
by: carlos | last post by:
I am working on a regular expression validation for my search page. What I have so far works for most cases, but I would like to fine tune it some. I am new to regular expressions, and I do not...
0
7051
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,...
1
6750
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...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
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,...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4493
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...
0
3003
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...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
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 ...

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.