List Info

Thread: Ignore any non-aphanumeric characters within matched text




Ignore any non-aphanumeric characters within matched text
user name
2006-10-20 18:54:52
Bo wrote:
> Yes, i would like those values of B to also match. B
could have any
> number of non-aphanumeric characters present in any
location.

Assume 'A' can contain only alpha-numeric, and no
metacharacter within
'A'.

so for an 'A' like '12345', you actually want a pattern
like:

 1W*2W*3W*4W*5

(Just for easy description, I used w instead of
[A-Za-z0-9], and W
instead of [^A-Za-z0-9] to specify alphanumerics and
non-alphanumerics...)

so that we can get:

match:   1###2---34-5---
nonmatch: 1##2*2--345

Thus, given an 'A', use the following replacement:

for (non-)word:
Pattern  :   (?<=w)(?=w)
Replace:  \W*

for (non-)alphanumeric:
Pattern:     (?<=[A-Za-z0-9])(?=[A-Za-z0-9])
Replace:    [^A-Za-z0-9]*

for example, in Perl, let

   $A = '12345';
   $B = '1(945)123-4567' ;
   ($A_ptn = $A) =~ s/(?<=w)(?=w)/\W*/g;
   print "Matchn" if $B =~ /$A_ptn/;

then $A_ptn is defined as:

   1W*2W*3W*4W*5

and $B = '1(945)123-4567' should be able to match the above
pattern..
it's quite similar if you are using Ruby.. Below is a short
Perl test
code(under Linux):

perl -wle '
    my $A = "12345";
    (my $A_ptn = $A) =~ s/(?<=w)(?=w)/\W*/g;
    my B = qw/
        1(234)566-6666
        ***1-2-34-5-****
        1***234^^56666
        1+++2---34-5---
        1++2*2--345
        12345
    /;
    print "Matched: $_" for grep {/$A_ptn/} B;
'
___result_____________
Matched: 1(234)566-6666
Matched: ***1-2-34-5-****
Matched: 1***234^^56666
Matched: 1+++2---34-5---
Matched: 12345
_____________

Good luck,
Xicheng


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://grou
ps-beta.google.com/group/regex
-~----------~----~----~----~------~----~------~--~---

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )