News for hackers. Stuff that matters.
Re:Advanced Regular Expression Tips and Techniques by tmslnz on Monday December 09, 2013 @03:00AM (#6873292)
My all-time favourite reference for regex syntax is http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt It's compact and clear enough for most cases.
Re:Advanced Regular Expression Tips and Techniques by gmac on Monday December 09, 2013 @07:47AM (#6874025)
If you want readable spacing and comments in JS RegExps, my customary plug: http://blog.mackerron.com/2010/08/08/extended-multi-line-js-...
Re:Advanced Regular Expression Tips and Techniques by esalman on Monday December 09, 2013 @07:49AM (#6874031)
Was going to ask that questions. Thanks.
Re:Advanced Regular Expression Tips and Techniques by draegtun on Thursday December 12, 2013 @11:05AM (#6895126)
These days I try and use Named Capture Buffers where feasible to make my regex code much easier to understand & update.

For eg. Here is one way I would do the first example shown (using Perl this time):

  use 5.016;
  use warnings;
  use re '/x';  # ignore all whitespace in regex patterns
 
  my $delimiter      = qr/ (?<delimiter>    [\-\.\s])      /;
  my $prefix         = qr/ (?<prefix>       1)             /;
  my $area_code      = qr/ (?<area_code>    \d{3})         /;
  my $first3digits   = qr/ (?<first3digits> \d{3})         /;
  my $last4digits    = qr/ (?<last4digits>  \d{4})         /;

  my $prefix_rule    = qr/ ($prefix $delimiter)            /;
  my $area_code_rule = qr/ ($area_code | \( $area_code \)) /;

  my $pattern = qr/
      ^
      ($area_code_rule | ($prefix_rule $area_code_rule))
      $delimiter
      $first3digits
      $delimiter
      $last4digits
      \Z
  /;
 
  my @numbers = (
      "123 555 6789",
      "1-(123)-555-6788",
      "(123-555-6787",
      "(123).555.6786",
      "123 55 6785",
  );
 
  for my $number (@numbers) {
      if ($number =~ $pattern) {
          print "$number is valid : ";
          say join "-", 
                  $+{prefix} || 'none', 
                  $+{area_code}, 
                  $+{first3digits}, 
                  $+{last4digits};
      } 
  } 
Output:

  123 555 6789 is valid : none-123-555-6789
  1-(123)-555-6788 is valid : 1-123-555-6788
  (123).555.6786 is valid : none-123-555-6786
Re:Advanced Regular Expression Tips and Techniques by dspillett on Monday December 09, 2013 @06:27AM (#6873821)
I'm getting "Panda Endpoint Protection has blocked access to this page, Reason:The page contains malware or exploits than could infect your PC." from my office machine - is/was there something there that I should inform the site owner about, or a false alarm that I should let Panda know about?
Re:Advanced Regular Expression Tips and Techniques by pypix on Monday December 09, 2013 @07:50AM (#6874036)
The site has no potential malware or virus, its clean. your Panda Antivirus is detecting it wrong.
Re:Advanced Regular Expression Tips and Techniques by leeoniya on Monday December 09, 2013 @12:25AM (#6872928)
i never understood why non-capturing syntax requires extra work - '(?:' vs '(' - seems very backwards.
Re:Advanced Regular Expression Tips and Techniques by pcmonk on Monday December 09, 2013 @12:51AM (#6872990)
I think it's simply because capturing groups are much more common than non-capturing groups. Even though non-capturing is "simpler", it's less common, so we default to capturing and use the more verbose syntax for non-capturing.
Re:Advanced Regular Expression Tips and Techniques by brudgers on Monday December 09, 2013 @10:39AM (#6874747)
Consider it as a compiler flag - a regex without capture and backtracking can be represented mathematicly as a regular expression (I.e. it can be translated into a DFA). Once a regex starts capturing, its representation requires a stack or other memory structure. Or a regex might or might not be a regular expression, strictly speaking (which isn't done all that often).
Re:Advanced Regular Expression Tips and Techniques by teddyh on Monday December 09, 2013 @09:56AM (#6874530)
Historical reasons. The capturing syntax came first, and when syntax for non-capturing groups was wanted, an otherwise illegal character sequence (? was chosen.