473,603 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ 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 1774
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
1243
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 finda way to return all results that do NOT include the "+"character and end with a specific extension. The issue seems tobe with the ".*" portion of the statement, but I am notsure how to just return palmpack.par2... filename to match:...
11
3892
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 parsed can completely wreck it. The string I am trying to parse is as follows: commandText=insert into (Text) values (@message + N': ' + @category);commandType=StoredProcedure; message=@message; category=@category I am looking to retrive name value...
3
1812
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 the following 3 lines: This is OK This is Not OK Again This is OK
7
3810
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
25
5143
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 (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
2
7254
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: ========================= Item 4.01 Regulation and other items <b>Item 4. Regulation</b>
7
3134
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 values returned are are 0,1,1 - the values should be 0,-1,-1. The positive lookahead expressiono RE=/(?=((00000)|(11111)))/ returns -1, 0, 0 respectively - this is correct
6
8364
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, -3). Current approach: I want to validate the user's entry during the TextChanged() event. During the validation, I need a function that will parse the string and see if it is a negative integer. Does anyone have suggestions on the best way to...
4
1625
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 have the time to read up some more on it. Can someone help? What I would like to do is allow words to be parsed using quotes. However, they can also include boolean searching. Lastly, I need to ensure the character's do not exceed a certain...
0
7928
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8415
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
8405
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8060
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8273
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5878
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3903
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
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2430
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

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.