473,320 Members | 1,724 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,320 software developers and data experts.

Searching for boolean variables with regular expressions.

Hi;

I need some simple help with my regular expressions.

I want to search my input text for all the boolean variables which do
not start with bln. i.e I want to match "bool followed by 1 or more
spaces followed by text other than bln" in my input text.
Eg:
1. private bool dispose; should be highlighted
2. private bool blndispose; should be not be highlighted

I have written the following regular expression text for this:
bool\s+[^bln] but it highlights both 1 and 2. Where could I be going
wrong??

Kind Regards;
Ajit Goel

Nov 17 '05 #1
2 2010
Your problem is that:

bool\s+[^bln]

Satifies strings starting with bool, One ore more space and than any string
that is not bln. But " bln" is not "bln"

You can use:

bool(\s)[^\s*bln]

"aj******@gmail.com" wrote:
Hi;

I need some simple help with my regular expressions.

I want to search my input text for all the boolean variables which do
not start with bln. i.e I want to match "bool followed by 1 or more
spaces followed by text other than bln" in my input text.
Eg:
1. private bool dispose; should be highlighted
2. private bool blndispose; should be not be highlighted

I have written the following regular expression text for this:
bool\s+[^bln] but it highlights both 1 and 2. Where could I be going
wrong??

Kind Regards;
Ajit Goel

Nov 17 '05 #2
In article <11**********************@z14g2000cwz.googlegroups .com>,
<aj******@gmail.com> wrote:

: I need some simple help with my regular expressions.
:
: I want to search my input text for all the boolean variables which do
: not start with bln. i.e I want to match "bool followed by 1 or more
: spaces followed by text other than bln" in my input text.
: Eg:
: 1. private bool dispose; should be highlighted
: 2. private bool blndispose; should be not be highlighted
:
: I have written the following regular expression text for this:
: bool\s+[^bln] but it highlights both 1 and 2. Where could I be going
: wrong??

Remember that [] creates a character class, so [^bln] means any
character that's not b, or l, or n. It doesn't mean, as you seem
to intend, any sequence other than "bln".

As another poster noted, you're forgetting that the matcher will
backtrack to try to find a match.

As we know, quantifiers (such as +) are greedy, so the first stab
at your case 2 is -- assuming you're using a monospaced font:

private bool blndispose;
^--- look for [^bln]

The spot where it's looking (indicated by the caret) is b, which
fails to match against [^bln].

Being the demagogue that it is, the matcher accuses the plus
quantifier of being too greedy and calls in jackbooted thugs to
forcibly take away one of its hard-won spaces:

private bool blndispose;
^--- look for [^bln]

A space is in fact neither b nor l nor n, so the match commissar is
happy on two counts: he scratched his collectivist itch (he did allow
the plus to match *some* spaces but made sure everyone else had enough)
and gets to report a match. Too bad it was a match we didn't want.

Below is one way to do it:

static void Main(string[] args)
{
string[] inputs = new string[]
{
"private bool dispose;",
"private bool blndispose;",
};

Regex unhungarian = new Regex(@"bool(?!\s+bln)");
foreach (string input in inputs)
{
string verdict =
unhungarian.IsMatch(input)
? "highlight" : "don't";

Console.WriteLine("'" + input + "': " + verdict);
}
}

This pattern says find me a bool that's not followed by a run of
spaces followed by bln.

Hope this helps,
Greg
Nov 17 '05 #3

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

Similar topics

6
by: Chris Lasher | last post by:
Hello, I would like to create a set of very similar regular expression. In my initial thought, I'd hoped to create a regular expression with a variable inside of it that I could simply pass a...
5
by: Richard Berg | last post by:
Hello, I need to search a byte array for a sequence of bytes. The sequence may include wildcards. For example if the array contains 0xAA, 0xBB, 0xAA, OxDD then I want to be able to search for...
5
by: Dan | last post by:
I wonder if anyone has suggestions for reducing the amount of time it would take to search my array using the function that I have written. I want to find a position in the array of an item that...
42
by: Dooglo | last post by:
I'm new VB and programming all together, but I'm getting he hang of it. My question is; I'm writting a program to figure square feet and yards when the user inputs "Length in feet and inch (...
7
by: Brian Mitchell | last post by:
Is there an easy way to pull a date/time stamp from a string? The DateTime stamp is located in different parts of each string and the DateTime stamp could be in different formats (mm/dd/yy or...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
10
by: Phil Latio | last post by:
How do I use wildcards when searching in array? At least that's what I think I need !! I have the line: if ($attribute != "id") The above is not 100% correct because it should also be...
4
by: Costa | last post by:
I am looking for a c/c++ text search engine library that supports: - free text searching - not only beginning of words but substrings as well - wildcard searching - I want strings such as...
2
by: Bart Kastermans | last post by:
I have a file in which I am searching for the letter "i" (actually a bit more general than that, arbitrary regular expressions could occur) as long as it does not occur inside an expression that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.