regex - Need help with Perl reg ex? -


here text file forms.

s1,f2  title including several white spaces  (abbr) single,here<->there,reply s1,f2  title including several white spaces  (abbr) single,here<->there s1,f2  title including several white spaces  (abbr) single,here<->there,[reply] 

how change reg ex work on 3 forms above?

/^s(\d),f(\d)\s+(.*?)\((.*?)\)\s+(.*?),(.*?)[,](.*?)$/ 

i tried replace (.*?)$/ [.*?]$/. doesn't work. guess shouldn't use [](square brackets) match possible word of [reply](including []).

actually, general question should how match possible characters better in reg exp using perl? looked online perldoc webpages. hard me find out useful information based on perl knowledge level. that's why asked stupid questions.

appreciated comments , suggestions.

what using negated character classes:

 /^s(\d),f(\d)\s+([^()]*?)\s+\(([^()]+)\)\s+([^,]*),([^,]*)(?:,(.*?))?$/ 

when incorporated script:

#!/bin/perl use strict; use warnings; while (<>) {     chomp;     my($s,$f,$title,$abbr,$single,$here,$reply) =         $_ =~ m/^s(\d),f(\d)\s+([^()]*?)\s+\(([^()]+)\)\s+([^,]*),([^,]*)(?:,(.*?))?$/;     $reply ||= "<no reply>";     print "s$s f$f <$title> ($abbr) $single : $here : $reply\n"; } 

and run on original data file, produces:

s1 f2 <title including several white spaces> (abbr) single : here<->there : reply s1 f2 <title including several white spaces> (abbr) single : here<->there : <no reply> s1 f2 <title including several white spaces> (abbr) single : here<->there : [reply] 

you should use 'xms' suffix expression allow document more easily:

#!/bin/perl use strict; use warnings;  while (<>) {     chomp;      my($s,$f,$title,$abbr,$single,$here,$reply) =         $_ =~ m/^                 s(\d) ,             # s1                 f(\d) \s+           # f2                 ([^()]*?) \s+       # title                 \(([^()]+)\) \s+    # (abbreviation)                 ([^,]*) ,           # single                 ([^,]*)             # here or there                 (?: , (.*?) )?      # optional reply                 $                /xms;      $reply ||= "<no reply>";     print "s$s f$f <$title> ($abbr) $single : $here : $reply\n"; } 

i confess i'm still apt write one-line monsters - i'm trying mend ways.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -