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?

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:


Open in new window

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:


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?
käµfm³d 👽Commented:
Honestly, I think the most obvious/readable would be to use alternation on the whole thing:


Open in new window

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

You might try this instead of my previous:


Open in new window

Not quite clean, but you don't have to repeat your symbol class.
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...


...but you would not match:

f*i%nance  (i.e. mixed symbols)
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..
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

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:


Open in new window

Enjoy! :-)

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

From novice to tech pro — start learning today.