Suppose I need to have a regex expression (using Perl regex) that, say, matches ab\q as simple text and does not interpret a as any number of a's. I know it is simple here, but I need it to work for ANY string of characters.

Recommended Answers

All 11 Replies

I actually had the * in this string when I wrote it and don't know how it went missing, but that is beside the point. My question is to make sure things stay as text. I don't care right now how to get multiples. I want to be able to match strings that have whatever regex metacharacters in them and still read as text strings. For instance, suppose my input string is "a*b+c/\/" and I want a Perl regex expression to match it. Howe do I do that, and do it in a way (meaning obviously generalizable) to match any string characters whatever as long as I know what that string is.

I see, then use the '\' (also known as the escape symbol) symbol. For example, if you want to match '*' character, one would do 'a*'.

Thank you for this answer, but I was hoping for something simpler in the general situation. I want to be able to take pieces of text that are definitely just text followed perhaps by some regex followed by some more text, etc. The thing is, all the text that shows up must be preserved as it shows up no matter what sequence of characters it contains that would be completely different if regarded as a regex string. I could parse the text, find all the metacharacters, and get rid of them using the . I guess that is not too terrible, but I was hoping there is something simpler that will work a large string of text no matter what it contains. I don't know if there is such a thing. Perhaps there isn't. I also see that, as usual, I have not been as clear about what I am asking as I should be. I hope this is now clear. If not, please ask me.

Hi achava,

Instead of matching your input indivdually, you could use this:

.+?  # see explaination below

'.' The dot matches any single character, or even newline when s modifier is used.
'+' The plus or cross [ whichever you chose to call it ] then matches "one or more of the character" that preseeded it.
'?' The question mark matches one or zero times of the character.

If you are using a '*', because, star tends to be greedy, that is matching zero or more times of the character, you should use a '?' after it.

You could check

   perldoc perlrequick or

For quick peruse of perl regex.
You could also post post the string you are trying to use regex on.

Hope this helps.

Let me clear this up a little. You want to read a whole string as a string without it being used as a regex or metacharacter? Something like:

This is an example of a regex: s/regex/texa/g. You can by pass it with the backspace like \\s.

What do you use the string for? As far as i know, it will read it as a normal string unless you use a regular expression on it like reading the line of string from a file and pipe it to the regular expression. Is that what you are doing?

suppose my input string is "a*b+c/\/" and I want a Perl regex expression to match it.

use warnings;
use strict;

my $x = 'String of 8 words including numbers and letters';
my $y = 'String including characters like a*b+c/\/ plus text';
my $z = 'Some numbers 7, 8 9 etc.';

my $substring = 'a*b+c/\/';

foreach my $string ($x, $y, $z){
    if ($string =~ m/\Q$substring\E/){
        print qq('$&' ), "found in '$string'\n";
        print q($substring ), "NOT found in '$string'\n";

See quotemeta

Thank you Wen Cai for continuing to try and answer my question. Here is what I am trying to do. Have a string of arbitrary text followed by a bit of regex follwed by another arbitrary text string followed by regex, etc. I need to turn this into regex (I do not get to program in Perl or anything else. I must take this collection of straight text strings and regex "strings" and turn them all into a regex "string" that matches the original string followed by the regex, etc.

For instance, suppose I want to determine if an input string contains the string "ab*d+\b3rs" after which I want to match using regex all the text that preceds the letter x. So I want a regex that matches the original string and then matches everything up to an x. If the original string is not in the input then there is no match. If there is no x following the initial string in the input, then there is no match. Otherwise there is a match as I said.

Now words to shock you. I know almost no Perl. I need to use Perl regex in this and turn the pattern I displayed for you into regex that matches the same thing. The main problem I have is creating a regex that will match the string presented. As you mentioned earlier, I could just erase all metacharacters using the \, but then I would have to write down all metacharacters and end up with annoyingly complicated code. It will work and I could do it, but I would rather not.

Thank you also d5e5. I will have to find a reference to Perl to understand what you had to say. I hope it works, but I just don't know.


Let twists d5e5 script a little, you should get want you want. The script below uses an infinite while loop, which ends when '$' was given by the user, this will enable the user to do as many matching as possible, without running the program again and again.
Then use an if loop within an if loop, with regex you should have want you want.
And like I said before, in case you are still not cleared, post the strings you really want to parse. There are several 'way to skin a cat' in Perl!

use warnings;
use strict;

my $string = 'ab*d+\b3rs';
my $input  = '';
do {
    print "Enter your Input: ";
    chomp( $input = <STDIN> );
    exit if $input eq '$';
    if ( $input =~ /\Q$string\E/ ) {
        print $input, " CONTAINS the original string ", $string, $/;
        if ( $input =~ /(\Q$string\E)x.+?$/ ) {
            print $1,
              " - MATCHES!!! - Original String IS followed immediately by x ",
        else {
" But still NO MATCH! - Original String was NOT followed Immediately by x \n";
    else { print $string, " not found in ", $input, $/; }
} while ( $input ne '$' );

I just read on a regex tutorial on the internet that if I want the regex to match a string S, then simply write \QS\E. If that is wrong for Perl regex please let me know. I will give it 24 hours and then declare the problem solved if not one answers and if my testing of this technique is successful.

Check the solution I just posted and let us know your result please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.