RegEx How to not match a string

J

jim_adams

I would like to match a string between AB that does not contain the
word "bad" and contains upto 5 characters ending in ";"

This is what I have so far, but it does not work. It does not group
the string "BAD" and instead matches any string that has "B", "A" or
"D" in it.
A(?<TAG>(([^;^(BAD)]{0,5})))B

For "ADDDDDB", <TAG> should contain "DDDDD"
For "AZZ;XXB", <TAG> should contain "ZZ;"
For "ABADB", <TAG> should be empty

Thanks,
Jim
 
D

Dr.Ruud

(e-mail address removed):
I would like to match a string between AB that does not contain the
word "bad" and contains upto 5 characters ending in ";"

This is what I have so far, but it does not work. It does not group
the string "BAD" and instead matches any string that has "B", "A" or
"D" in it.
A(?<TAG>(([^;^(BAD)]{0,5})))B

For "ADDDDDB", <TAG> should contain "DDDDD"
For "AZZ;XXB", <TAG> should contain "ZZ;"
For "ABADB", <TAG> should be empty

Do it in 2 steps, step 1 being to remove any internal 'BAD'.

And anchor the starting A and ending B.

Step 2: s/^A(([^;]){1,4}[\2;]?).*B$/$1/

The [\2;] can also be written as (?:\2|;).

(untested)
 
M

Matt Garrish

I would like to match a string between AB that does not contain the
word "bad" and contains upto 5 characters ending in ";"

This is what I have so far, but it does not work. It does not group
the string "BAD" and instead matches any string that has "B", "A" or
"D" in it.
A(?<TAG>(([^;^(BAD)]{0,5})))B

That looks suspiciously like a .Net regular expression. It certainly isn't
Perl. All I can tell you is that you can't use a character class like that
(character classes are for characters, oddly enough). If you were using Perl
I would suggest you look up negative lookahead assertions in perlre. As I
doubt you are, you should try a group that specializes in whatever language
you're using.

Matt
 
T

Tad McClellan

For "ADDDDDB", <TAG> should contain "DDDDD"


Variables in Perl start with a dollar sign.

<TAG> cannot "contain" anything.

What programming language are you using?

Try posting actual Perl code *that we can run*.
 
T

Tad McClellan

I would like to match a string between AB that does not contain the
word "bad" and contains upto 5 characters ending in ";"

This is what I have so far, but it does not work.


Of course not, it does not even compile!

It does not group
the string "BAD" and instead matches


It can never match anything if it cannot even execute!
any string that has "B", "A" or
"D" in it.


That is what you told it to do.

If that isn't what you want, then tell it to do something else.

A(?<TAG>(([^;^(BAD)]{0,5})))B

For "ADDDDDB", <TAG> should contain "DDDDD"
For "AZZ;XXB", <TAG> should contain "ZZ;"
For "ABADB", <TAG> should be empty


------------------------
while ( <DATA> ) {
chomp;
foreach my $match ( /A([^;]{0,5};?).*B/g ) {
print "$match\n" unless $match =~ /bad/i;
}
}

__DATA__
ADDDDDB
AZZ;XXB
ABADB
------------------------



Your stealthyness has earned you a spot in the scorefile. So long.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top