• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

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?

6 Solutions
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.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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! :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now