Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Regular expression needed

Posted on 2006-06-28
20
337 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

856 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