PCRE regular expression

Hello all,

Im new to PCRE regex syntax and i was wondering could i get a bit of help. Im trying to match the following target strings:

finance | f.inance | fi.nance | fin.ance etc

i.e the full stop can occur anywhere in the the target string. And i want the regex to match all possibilities.. How can i achieve this with one regex?

Thanks,
oggiemcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Try:

\.?f\.?i\.?n\.?a\.?n\.?c\.?e\.?

Open in new window

0
oggiemcAuthor Commented:
That works kaufmed..Im just wondering though is there a cleaner way to do this? i.e without having to put the search pattern after each character? also, i will be searching for all punctuation characters so i would have to have something like the following:

[.*^%$£]?f[.*^%$£]?i[.*^%$£]?n[.*^%$£]?

Which begins to look quite messy..is there no way of putting the search characters i.e [.*^%$£]at the end of the regex and then specify a global search in the target string?
0
käµfm³d 👽Commented:
Honestly, I think the most obvious/readable would be to use alternation on the whole thing:

finance|f[.*^%$£]inance|fi[.*^%$£]nance|fin[.*^%$£]ance|fina[.*^%$£]nce|finan[.*^%$£]ce|financ[.*^%$£]e

Open in new window


...but I can see how that might get unwieldy with larger words.

You might try this instead of my previous:

finance|(?=.*?([.*^%$£]))f\1?i\1?n\1?a\1?n\1?c\1?e

Open in new window


Not quite clean, but you don't have to repeat your symbol class.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

käµfm³d 👽Commented:
A note on the above:  The following would also match...

f*i*n*a*n*c*e
fin%anc%e

...but you would not match:

f*i%nance  (i.e. mixed symbols)
0
oggiemcAuthor Commented:
Thanks for replies kaufmed..You have given me a few options there..I will leave question open for the meantime just to see if there are any other suggestions..
0
ozoCommented:
^(?=[^\W_]+[.*^%$£]?[^\W_]+$)f\W?iW?n\W?a\W?n\W?c\W?e$
0
wilcoxonCommented:
My first thought is to use alternation and build the regex programmatically.  Assuming, there can only be one punctuation in the string, this should work (if there can be multiple punctuation in the string then it would need modification):
my $string = 'finance';
my $punc = '[.*^%$£]';
my @letters = split //, $string;
my @opts;
my $max = @letters - 1;
for my $i (0..$max-1) {
    push @opts, join('', @letters[0..$i], $punc, $letters[$i+1..$max]);
}
my $rx = join '|', @opts;
my $rx = qr{$rx};

Open in new window

0
Derek JensenCommented:
A note on the above (by kaufmed) on f*i%nance:

I found a working regex that finds that, and any other combination of symbols:



finance|finance(?<=[.*^%$£])

Open in new window

Enjoy! :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.