Sponsored Content
Top Forums Shell Programming and Scripting Perl - Use of *? in Matching Pattern Post 302945322 by drl on Thursday 28th of May 2015 10:15:16 AM
Old 05-28-2015
Hi.
Code:
       By default, a quantified subpattern is "greedy", that is, it will match
       as many times as possible (given a particular starting location) while
       still allowing the rest of the pattern to match.  If you want it to
       match the minimum number of times possible, follow the quantifier with
       a "?".  Note that the meanings don't change, just the "greediness":

           *?     Match 0 or more times, not greedily
           +?     Match 1 or more times, not greedily
           ??     Match 0 or 1 time, not greedily
           {n}?   Match exactly n times, not greedily
           {n,}?  Match at least n times, not greedily
           {n,m}? Match at least n but not more than m times, not greedily

-- from perlre: perldoc perlre, or man perlre q.v.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

pattern matching + perl question

i can only find the first occurance of a pattern how do i set it to loop untill all occurances have changed. #! /usr/bin/perl use POSIX; open (DFH_FILE, "./dfh") or die "Can not read file ($!)"; foreach (<DFH_FILE>) { if ($_ !~ /^#|^$/) { chomp; ... (1 Reply)
Discussion started by: Optimus_P
1 Replies

2. Shell Programming and Scripting

perl pattern matching

hi i am trying to get digits inside brackes from file , whose structure is defined below CREATE TABLE TELM (SOC_NO CHAR (3) NOT NULL, TXN_AMOUNT NUMBER (17,3) SIGN_ON_TIME CHAR (8) TELLER_APP_LIMIT NUMBER (17,3) FIL01 ... (2 Replies)
Discussion started by: zedex
2 Replies

3. Shell Programming and Scripting

Perl Pattern Matching !!! Help

Hello I got the below one from in one of this forums For Ex: Loading File System Networking in nature now i need to extract the patterns between the words File and Networking : i.e. sample output: System cmd used : cat <file> | sed 's/.*File //' | sed 's/Closing.*$//' Actually... (0 Replies)
Discussion started by: maxmave
0 Replies

4. Shell Programming and Scripting

Perl -Pattern Matching help..!

Hi, I got doubt in Pattern matching, could you tell me how the following differs in action ?? if ( $line1==/$line2/ ) if ( $line1=~/$line2/ ) if ( $line1=~m/$line2/) What is the significance of '~' in matching. Thanks in advance CoolBhai (5 Replies)
Discussion started by: coolbhai
5 Replies

5. Shell Programming and Scripting

Perl Pattern Matching

Hello experts, I have a file containing the following text(shortened here). File Begin ---------- < # Billboard.d3fc1302a677.imagePath=S:\\efcm_T4 < Billboard.d3fc1302a677.imagePath=S:\\efcm_T4 --- > # Billboard.d3fc1302a677.imagePath=S:\\efcm_Cassini >... (2 Replies)
Discussion started by: nmattam
2 Replies

6. Shell Programming and Scripting

Perl pattern matching!!

Hi experts, I have many occurances of the following headers in a file. I need to grep for the word changed/inserted in the header, calculate the difference between the two numbers and list the count incrementally. Headers in a file look like this: ------------------- ---------------------... (6 Replies)
Discussion started by: nmattam
6 Replies

7. Shell Programming and Scripting

Perl Pattern matching...

I am doing a file patterhn matching for a text file in PERL I am using this,,, but it says that no file is found $filepattern = '\d{1,4}.*A0NW9693.NDM.HBIDT.*.AD34XADJ.txt'; Can anyone help me out with Perl Pattern Matching concepts and how to do pattern matching for this txt file:... (4 Replies)
Discussion started by: msrahman
4 Replies

8. Shell Programming and Scripting

Pattern Matching in PERL

I have a 2 files in .gz format and it consists of 5 million lines the format of the file would be gzcat file1.gz | more abcde aerere ffgh56 .. .. 12345 gzcat file2.gz | more abcde , 12345 , 67890, ffgh56 , 45623 ,12334 whatever the string is in the file1 should be matched... (3 Replies)
Discussion started by: aravindj80
3 Replies

9. Shell Programming and Scripting

Need help with perl pattern matching

My log file looks as given below, its actually a huge file around 1 GB and these are some of the line: conn=5368758 op=10628050 msgId=64 - RESULT err=0 tag=101 nentries=1 etime=0 conn=7462122 op=-1 msgId=-1 - fd=247 slot=247 LDAPS connection from 10.13.18.12:37645 to 10.18.6.45 conn=7462122... (5 Replies)
Discussion started by: sags007_99
5 Replies

10. Shell Programming and Scripting

Pattern matching in Perl

Hi, I have a list of IP, eg : 192.168.0.15 192.168.0.24 192.168.2.110 192.168.2.200 And I would like the shortest pattern who match with '192.168.0' and '192.168.2' (without the last dot and number). (7 Replies)
Discussion started by: X-Or
7 Replies
PERLREREF(1)						 Perl Programmers Reference Guide					      PERLREREF(1)

NAME
perlreref - Perl Regular Expressions Reference DESCRIPTION
This is a quick reference to Perl's regular expressions. For full information see perlre and perlop, as well as the "SEE ALSO" section in this document. OPERATORS "=~" determines to which variable the regex is applied. In its absence, $_ is used. $var =~ /foo/; "!~" determines to which variable the regex is applied, and negates the result of the match; it returns false if the match succeeds, and true if it fails. $var !~ /foo/; "m/pattern/msixogc" searches a string for a pattern match, applying the given options. m Multiline mode - ^ and $ match internal lines s match as a Single line - . matches i case-Insensitive x eXtended legibility - free whitespace and comments o compile pattern Once g Global - all occurrences c don't reset pos on failed matches when using /g If 'pattern' is an empty string, the last successfully matched regex is used. Delimiters other than '/' may be used for both this operator and the following ones. The leading "m" can be omitted if the delimiter is '/'. "qr/pattern/msixo" lets you store a regex in a variable, or pass one around. Modifiers as for "m//", and are stored within the regex. "s/pattern/replacement/msixogce" substitutes matches of 'pattern' with 'replacement'. Modifiers as for "m//", with one addition: e Evaluate 'replacement' as an expression 'e' may be specified multiple times. 'replacement' is interpreted as a double quoted string unless a single-quote ("'") is the delimiter. "?pattern?" is like "m/pattern/" but matches only once. No alternate delimiters can be used. Must be reset with reset(). SYNTAX Escapes the character immediately following it . Matches any single character except a newline (unless /s is used) ^ Matches at the beginning of the string (or line, if /m is used) $ Matches at the end of the string (or line, if /m is used) * Matches the preceding element 0 or more times + Matches the preceding element 1 or more times ? Matches the preceding element 0 or 1 times {...} Specifies a range of occurrences for the element preceding it [...] Matches any one of the characters contained within the brackets (...) Groups subexpressions for capturing to $1, $2... (?:...) Groups subexpressions without capturing (cluster) | Matches either the subexpression preceding or following it 1, 2, 3 ... Matches the text from the Nth group ESCAPE SEQUENCES These work as in normal strings. a Alarm (beep) e Escape f Formfeed Newline Carriage return Tab 37 Any octal ASCII value x7f Any hexadecimal ASCII value x{263a} A wide hexadecimal value cx Control-x N{name} A named character l Lowercase next character u Titlecase next character L Lowercase until E U Uppercase until E Q Disable pattern metacharacters until E E End modification For Titlecase, see "Titlecase". This one works differently from normal strings:  An assertion, not backspace, except in a character class CHARACTER CLASSES [amy] Match 'a', 'm' or 'y' [f-j] Dash specifies "range" [f-j-] Dash escaped or at start or end means 'dash' [^f-j] Caret indicates "match any character _except_ these" The following sequences work within or without a character class. The first six are locale aware, all are Unicode aware. See perllocale and perlunicode for details. d A digit D A nondigit w A word character W A non-word character s A whitespace character S A non-whitespace character C Match a byte (with Unicode, '.' matches a character) pP Match P-named (Unicode) property p{...} Match Unicode property with long name PP Match non-P P{...} Match lack of Unicode property with long name X Match extended Unicode combining character sequence POSIX character classes and their Unicode and Perl equivalents: alnum IsAlnum Alphanumeric alpha IsAlpha Alphabetic ascii IsASCII Any ASCII char blank IsSpace [ ] Horizontal whitespace (GNU extension) cntrl IsCntrl Control characters digit IsDigit d Digits graph IsGraph Alphanumeric and punctuation lower IsLower Lowercase chars (locale and Unicode aware) print IsPrint Alphanumeric, punct, and space punct IsPunct Punctuation space IsSpace [sck] Whitespace IsSpacePerl s Perl's whitespace definition upper IsUpper Uppercase chars (locale and Unicode aware) word IsWord w Alphanumeric plus _ (Perl extension) xdigit IsXDigit [0-9A-Fa-f] Hexadecimal digit Within a character class: POSIX traditional Unicode [:digit:] d p{IsDigit} [:^digit:] D P{IsDigit} ANCHORS All are zero-width assertions. ^ Match string start (or line, if /m is used) $ Match string end (or line, if /m is used) or before newline  Match word boundary (between w and W) B Match except at word boundary (between w and w or W and W) A Match string start (regardless of /m)  Match string end (before optional newline) z Match absolute string end G Match where previous m//g left off QUANTIFIERS Quantifiers are greedy by default -- match the longest leftmost. Maximal Minimal Allowed range ------- ------- ------------- {n,m} {n,m}? Must occur at least n times but no more than m times {n,} {n,}? Must occur at least n times {n} {n}? Must occur exactly n times * *? 0 or more times (same as {0,}) + +? 1 or more times (same as {1,}) ? ?? 0 or 1 time (same as {0,1}) There is no quantifier {,n} -- that gets understood as a literal string. EXTENDED CONSTRUCTS (?#text) A comment (?:...) Groups subexpressions without capturing (cluster) (?imsx-imsx:...) Enable/disable option (as per m// modifiers) (?=...) Zero-width positive lookahead assertion (?!...) Zero-width negative lookahead assertion (?<=...) Zero-width positive lookbehind assertion (?<!...) Zero-width negative lookbehind assertion (?>...) Grab what we can, prohibit backtracking (?{ code }) Embedded code, return value becomes $^R (??{ code }) Dynamic regex, return value used as regex (?(cond)yes|no) (?(cond)yes) Conditional expression, where "cond" can be: (N) subpattern N has matched something (?{code}) code condition VARIABLES $_ Default variable for operators to use $* Enable multiline matching (deprecated; not in 5.9.0 or later) $` Everything prior to matched string $& Entire matched string $' Everything after to matched string The use of $`, $& or $' will slow down all regex use within your program. Consult perlvar for "@-" to see equivalent expressions that won't cause slow down. See also Devel::SawAmpersand. If you upgrade to Perl 5.10, you can also use the equivalent variables "${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}", but for them to be defined, you have to specify the "/p" (preserve) modifier on your regular expression. $1, $2 ... hold the Xth captured expr $+ Last parenthesized pattern match $^N Holds the most recently closed capture $^R Holds the result of the last (?{...}) expr @- Offsets of starts of groups. $-[0] holds start of whole match @+ Offsets of ends of groups. $+[0] holds end of whole match Captured groups are numbered according to their opening paren. FUNCTIONS lc Lowercase a string lcfirst Lowercase first char of a string uc Uppercase a string ucfirst Titlecase first char of a string pos Return or set current match position quotemeta Quote metacharacters reset Reset ?pattern? status study Analyze string for optimizing matching split Use a regex to split a string into parts The first four of these are like the escape sequences "L", "l", "U", and "u". For Titlecase, see "Titlecase". TERMINOLOGY Titlecase Unicode concept which most often is equal to uppercase, but for certain characters like the German "sharp s" there is a difference. AUTHOR
Iain Truskett. Updated by the Perl 5 Porters. This document may be distributed under the same terms as Perl itself. SEE ALSO
o perlretut for a tutorial on regular expressions. o perlrequick for a rapid tutorial. o perlre for more details. o perlvar for details on the variables. o perlop for details on the operators. o perlfunc for details on the functions. o perlfaq6 for FAQs on regular expressions. o perlrebackslash for a reference on backslash sequences. o perlrecharclass for a reference on character classes. o The re module to alter behaviour and aid debugging. o "Debugging regular expressions" in perldebug o perluniintro, perlunicode, charnames and perllocale for details on regexes and internationalisation. o Mastering Regular Expressions by Jeffrey Friedl (http://regex.info/) for a thorough grounding and reference on the topic. THANKS
David P.C. Wollmann, Richard Soderberg, Sean M. Burke, Tom Christiansen, Jim Cromie, and Jeffrey Goff for useful advice. perl v5.8.9 2007-11-17 PERLREREF(1)
All times are GMT -4. The time now is 02:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy