Solved

Regular expression needed

Posted on 2006-06-28
20
313 Views
Last Modified: 2008-02-26
I have a form field. In this field you may no enter something that will result in a search for the letters FG on their own
Anything letter or digit in front or any letter or digit after FG is fine

Valid queries:

AFG
AFG01
FGA
FGA01
FG1
FG1 OR FG2

The current regexp is this:

^FG$|[ ]FG$|[ ]FG[ ]

However in this I am able to search using
(AB OR FG)
and also ((((FG XXX)))
which I need to be not allowed.

Thanks

Michel
0
Comment
Question by:Michel Plungjan
  • 7
  • 4
  • 3
  • +3
20 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 17000315
Hi mplungjan,
What about
^\w*FG\w*$

(Assuming you want any number of letter/digit before FG)

Enjoy,

Stefan
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 17000498
Here is my test script.

Seems your regexp is not complete
<script>
reg = /^\w*FG\w*$/
function testReg(str,valid) {
  v = (valid)?"Valid:":"Not Valid"
  test = reg.test(str);
  p = ((test && valid) || (!test && !valid))?"passed":"not passed"
  txt = "<hr>"+v+" |"+str+"| "+p+ " ("+test+")"
  document.write(txt)
}

testReg('FG',0)
testReg(' FG',0)
testReg('(AB OR FG)',0)
testReg('(AB OR FG0)',1)
testReg('FG0',1)
testReg('AFGB',1)
testReg(' FG0',1)

</script>

I get

Not Valid |FG| not passed (true) <<<<<<<
Not Valid | FG| passed (false)
Not Valid |(AB OR FG)| passed (false)
Valid: |(AB OR FG0)| not passed (false) <<<<<<<
Valid: |FG0| passed (true)
Valid: |AFGB| passed (true)
Valid: | FG0| not passed (false) <<<<<<<<
0
 
LVL 4

Expert Comment

by:NorCal2612
ID: 17000597
Would this work?

#!/usr/bin/perl

$in = shift;

if ($in =~ /(\d|\w)FG(\d|\w)/ig) { print "OK\n"; } else { print "Not OK\n"; }

# end script

Good luck! :D
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 17000734
Please plug it into my test page and see it does not work:

Not Valid |FG| passed (false)
--------------------------------------------------------------------------------
Not Valid | FG| passed (false)
--------------------------------------------------------------------------------
Not Valid |(AB OR FG)| passed (false)
--------------------------------------------------------------------------------
Valid: |(AB OR FG0)| not passed (false) <<<<
--------------------------------------------------------------------------------
Valid: |FG0| not passed (false) <<<<<<<<<
--------------------------------------------------------------------------------
Valid: |AFGB| passed (true)
--------------------------------------------------------------------------------
Valid: | FG0| not passed (false)  <<<<<<<
0
 
LVL 4

Expert Comment

by:NorCal2612
ID: 17000783
Ok sorry I misread your initial requirements slightly. This worked for me in all cases:

#!/usr/bin/perl

$in = shift;

if ($in =~ /(\d|\w)FG/ig || $in =~ /FG(\d|\w)/ig) { print "OK\n"; } else { print "Not OK\n"; }
0
 
LVL 12

Expert Comment

by:stefan73
ID: 17000847
mplungjan,

I'm not quite sure for which cases you need a match and which must explicitly fail... ...,0 must succeed, ,1 must fail?

> testReg('FG',0)
> testReg(' FG',0)
> testReg('(AB OR FG)',0)
> testReg('(AB OR FG0)',1)
> testReg('FG0',1)
> testReg('AFGB',1)
> testReg(' FG0',1)



Stefan
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 17000895
Here is a version with my latest suggestion - please note it is a NEGATIVE test - my test does not pass all examples

<script>
//reg = /^\w*FG\w*$/; // positive test
//reg = /(\d|\w)FG(\d|\w)/ig; // positive test
reg = /^FG$|[ ]FG[ ]|FG[^a-z0-9]/; // negative test

function testReg(str,valid) {
  v = (valid)?"Valid:":"Not Valid"
  test = reg.test(str);
//  p = ((test && valid) || (!test && !valid))?"passed":"not passed"; // positive test
  p = ((test && !valid) || (!test && valid))?"passed":"not passed"; // negative test
  txt = "<hr>"+v+" |"+str+"| "+p+ " ("+test+")"
  document.write(txt)
}

testReg('FG',0); // not valid
testReg(' FG',0); // not valid
testReg(' FG ',0); // not valid
testReg('(AB OR FG)',0); // not valid
testReg('(AB OR FG0)',1); // valid
testReg('FG0',1); // valid
testReg('AFGB',1); // valid
testReg(' FG0',1); // valid

</script>
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 17001423
ALmost

<script>
//reg = /^\w*FG\w*$/; // positive test
//reg = /(\d|\w)FG(\d|\w)/ig; // positive test
reg = /^FG$|[ ]FG[ ]|^FG[ ]|^[ ]FG$|FG[^a-z0-9]/; // negative test

function testReg(str,valid) {
  v = (valid)?"Valid:":"Not Valid"
  test = reg.test(str);
//  p = ((test && valid) || (!test && !valid))?"passed":"not passed"; // positive test
  p = ((test && !valid) || (!test && valid))?"passed":"not passed"; // negative test
  txt = "<hr>"+v+" |"+str+"| "+p+ " ("+test+")"
  document.write(txt)
}

testReg('FG',0)
testReg(' FG',0)
testReg(' FG ',0)
testReg('(AB OR FG)',0)
testReg('(AB OR FG0)',1)
testReg('FG0',1)
testReg('AFGB',1); <<<<<<< does not pass !!!
testReg(' FG0',1)

</script>
0
 
LVL 39

Expert Comment

by:Adam314
ID: 17001773
This doesn't look quite like perl code....  So i tested it using perl, and every one passed:

#NOTE: If it matches this pattern, it is NOT valid
if(  /\bFG\b/  ){
    print "NOT VALID";
}
else {
    print "Valid";
}


Here is the perl code:
#!/usr/bin/perl
use strict;

sub testReg {
      my ($str, $valid) = @_;
      my $v = ($valid)?"    Valid":"Not Valid";
      my $test = ($str =~ /\bFG\b/);
      my $p = ((!$test && $valid) || ($test && !$valid))?"    passed":"not passed";
      print "$v: $p: $str\n";
}

testReg('FG',0);
testReg(' FG',0);
testReg(' FG ',0);
testReg('(AB OR FG)',0);
testReg('(AB OR FG0)',1);
testReg('FG0',1);
testReg('AFGB',1);
testReg(' FG0',1);
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17002009
Try it this way:

#!/usr/bin/perl -w
use strict;

while (<DATA>) {
    if (/^FG$|(?<=[\s"'(])FG(?=[\s'")])/) {
        chomp;
        print "$_ not allowed\n";
    }
}
__DATA__
FG
AFG
AFG01
FGA
FGA01
FG1
FG1 OR FG2
(FG OR AB)
(AB OR FG)
(AB OR FG0)
"FG"
(FG)
'FG'

The output is:

FG not allowed
(FG OR AB) not allowed
(AB OR FG) not allowed
"FG" not allowed
(FG) not allowed
'FG' not allowed

all other lines are allowed.  I assumed you would like to disallow 'FG' and "FG", if this is an incorrect assumption, you can remove the ' and the " from the character classes used by the lookbehid and lookahead assertions.

Will this do what you need?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 17

Expert Comment

by:mjcoyne
ID: 17002756
Actually, you could tighten that up a bit further by using:

#!/usr/bin/perl -w
use strict;

while (<DATA>) {
    if (/^\s*FG\s*$|(?<=[\s"'(])FG(?=[\s'")])/) {
        chomp;
        print "$_ not allowed\n";
    }
}
__DATA__
FG
AFG
AFG01
FGA
FGA01
FG1
FG1 OR FG2
(FG OR AB)
(AB OR FG)
(AB OR FG0)
"FG"
(FG)
'FG'
 FG
FG
 FG
'FG)

instead.  Changing it to ^\s*FG\s*$ will disallow FG preceded or followed by nothing but spaces (either <space>FG, FG<space>, and <space>FG<space>, if on a line by themselves, will also be disallowed).
0
 
LVL 84

Expert Comment

by:ozo
ID: 17003153
while( <DATA> ){
    if( /(?<!\w)FG(?!\w)/ ){
        chomp;
        print "$_ not allowed\n";
    }
}
__DATA__
AFG
AFG01
FGA
FGA01
FG1
FG1 OR FG2
(FG OR AB)
(AB OR FG)
(AB OR FG0)
"FG"
(FG)
'FG'
 FG
FG
 FG
'FG)
0
 
LVL 84

Expert Comment

by:ozo
ID: 17003235
/^\s*FG\s*$|(?<=[\s"'(])FG(?=[\s'")])/
fails on
FG A
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 17004816
It is not perl because I did not ask for perl. Just a regexp. So please no perl or chomp
I am not using perl but a java parser - the regex experts sit here and most regexps out there are perl 5 compliant.

thanks for any help

Michel
0
 
LVL 84

Expert Comment

by:ozo
ID: 17005037
/(?<!\w)FG(?!\w)/
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 17005072
I figured that was the reason... and gave a regex that worked on the examples provided.

have you tried   /\bFG\b/    (if the string matches, it is NOT valid)
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17005646
Chomp is not necessary for the regular expression I provided, it's just in a script that tests the regex, which is (/^\s*FG\s*$|(?<=[\s"'(])FG(?=[\s'")])/).

Does (/^\s*FG\s*$|(?<=[\s"'(])FG(?=[\s'")])/) work for you?
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17005659
oops -- remove the parentheses left over from the "if" statement...

Does /^\s*FG\s*$|(?<=[\s"'(])FG(?=[\s'")])/ work for you?
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 17007386
Seems Adam has the lead so far.
http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_21901845.html#17001773

seems to work perfectly. I will run it through some more tests but that is very promising
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 17008715
Thanks all.

Very simple and effective solution

Michel
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now