Horje
perl regex Code Example
[Solved] - perl regex Code Example
Try following codes
perl regex
# Perl regex metacharacters:

| Symbol |         Meaning         |
|:------:|:-----------------------:|
|    .   |      any character      |
|   \w   |    alphanumeric and _   |
|   \W   |  any non-word character |
|   \s   |      any whitespace     |
|   \S   |    any non-whitespace   |
|   \d   |   any digit character   |
|   \D   | any non-digit character |
|   \t   |           tab           |
|   \n   |         newline         |
|    *   |  match 0 or more times  |
|    +   |  match 1 or more times  |
|    ?   |    match 0 or 1 times   |
|   {n}  |  match exactly n times  |
|  {n,m} |    match n to m times   |
|    ^   |     match from start    |
|    $   |       match to end      |

# Note: 
#	- use square brackets to match any of a set of characters, like [ACGT]
# 	- use ^ inside square brackets to negate matching those characters (i.e.
#		when you don't want to match any of them)
#	- use - to specify a character range, e.g. [a-d] to match any of a, b, c, d

# Example usage:
if ($dna_seq =~ m/^ATGCC[ACGT]GGN{6,9}(TAG|TGA|TAA)$/) {print "It's a match"};
# Where this will match any $dna_seq that starts with ATGCC, followed by one
#	character from ACGT, followed by GG, followed by 6-9 N characters, and
#	ending with TAG, TGA, or TAA
perl match regex
# Basic syntax:
$your_string =~ /regex pattern to match/
# Use !~ for not matching

# Example usage:
# Check if the regex pattern your is found in the variable $your_string
my $your_string = "this is your string";
if ($your_string =~ /your/) {
	print "your_string contains your\n"
}
--> your_string contains your

# Check if the regex pattern y?o*u+r is found in the variable $your_string
my $your_string = "this is your string";
if ($your_string =~ /y?o*u+r/) {
	print "your_string matches pattern\n"
}
--> your_string matches pattern
# Where, in regex:
# 	- ? matches 0 or 1 of the preceeding characters
#	- * matches 0 or more of the preceeding characters
#	- + matches 1 or more of the preceeding characters
Date: 2022-05-21 05:02:10
Category: perl
More for You