Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i try to match expression but not match

What I have tried:

<pre>#!/usr/bin/perl
use warnings;
use strict;
my $s = "Regular expression";
$s =~ /Regular(.*?)/;
print $s;
Posted
Updated 22-Jul-22 6:38am

Quote:
hi i try to match expression but not match

It works :
!
Try
PERL
$s =~ /Regular(.*)/;

Pay attention to the '?' removed, it may give better results.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
 
Share this answer
 
v2
Comments
[no name] 22-Jul-22 10:32am    
but is in python ???? i work in perl
Patrice T 22-Jul-22 10:41am    
RegEx are RegEx, only few detail change between languages.
Does it work ?
That works fine for me, so I cannot see what you may be doing wrong. You also need to understand that the expression $s =~ /Regular(.*?)/ returns 1 for a match, and nothing for a non-match.
Match:
PERL
my $s = "Regular expression";
print $s =~ /Regular(.*?)/ . "\n";

// output:
$ perl perltest.pl
1
$
Not match:
PERL
my $s = "Dogular expression";
print $s =~ /Regular(.*?)/ . "\n";

// output:
$ perl perltest.pl
 
$ 
 
Share this answer
 
v2
Comments
[no name] 22-Jul-22 11:08am    
thanks for rply but if i want return not 1 but the word espression ? how is possible to do that ?
Richard MacCutchan 22-Jul-22 11:22am    
You get what the =~ operator provides. If you want to capture data then see perlre - Perl regular expressions - Perldoc Browser[^].
[no name] 22-Jul-22 12:29pm    
i am novice where i find it ??
Richard MacCutchan 22-Jul-22 12:30pm    
I don't know, I have not read the document. Google may find you some simpler examples.
To assign matched substrings you have to use the substitute regex operator
PERL
my $s = "Regular expression";
$s =~ s/Regular(.*?)/$1/; # note leading s for regex operation
# update: (oops!)  changed assignment (=) to match-assign (=~) in above statement
#update 2:removed spurious $ from match expression
#        was previously /$Regular(.*?)/$1/ Wrong!
print "s = '$s'\n";

Note that $s includes the the whitespace following the matched text, which may not be what you want. But this example should get you going. If you don't want to change the value of $s then the following might be useful
PERL
$s = "Regular expression";
($t = $s) =~ s/Regular(.*?)/$1/;

print "s = '$s'\n";
print "t = '$t'\n";
produces
s = 'Regular expression'
t = ' expression'
 
Share this answer
 
v3
Comments
Richard MacCutchan 23-Jul-22 4:02am    
When I try the code in your first example I get:
Global symbol "$Regular" requires explicit package name (did you forget to declare "my $Regular"?) at perltest.pl line 5.
Execution of perltest.pl aborted due to compilation errors.

Any idea what I am doing wrong?
k5054 23-Jul-22 6:57am    
Nothing. That $ before Regular is a typo too. Of course, it works fine if you you don't have use strict; in effect.
Richard MacCutchan 23-Jul-22 14:10pm    
Thanks. I assumed it was there to indicate start searching for a match at the beginning of the string.
[no name] 25-Jul-22 3:31am    
thanks for rply, anoter question suppose you have a phrases like this Regular expression bao bab lapos you want macth only expression why if i modify the regexp in this mode not work ? $s =~ s/Regular(.*?) b/$1/; thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900