Professional Web Applications Themes

OR in a regex - PERL Beginners

Hi Folks, Struggling to understand what is happening here... Example of text is Description Supports the management of the process to create and complete an order Actors SYSTEM Pre-Conditions A Service Instance may have been selected for the purchase This regex extracts the text /Description([^.]+)(?:Actors)/ Supports the management of the process to create and complete an order In some cases, the word "Actors" that I want to terminate on is the word "Steps" instead, so I though I'd use an OR inside parenthesis with no backreferences ?: to match either Actors or Steps - this now matches neither, so the ...

  1. #1

    Default OR in a regex

    Hi Folks,

    Struggling to understand what is happening here...

    Example of text is

    Description Supports the management of the process to create and
    complete an order Actors SYSTEM Pre-Conditions A Service Instance may
    have been selected for the purchase

    This regex extracts the text
    /Description([^.]+)(?:Actors)/
    Supports the management of the process to create and complete an order

    In some cases, the word "Actors" that I want to terminate on is the
    word "Steps" instead, so I though I'd use an OR inside parenthesis
    with no backreferences ?: to match either Actors or Steps - this now
    matches neither, so the pattern just runs to the end of the text

    (?<!Change )Description([^.]+)(?:Actors|Steps)

    any hints?

    --
    Colm
    Colm Guest

  2. #2

    Default Re: OR in a regex

    Oops, that example I gave actually works
    It's always better to give the whole problem...

    (?<!Change )Description([^.]+)(?:Actors|Pre-Conditions|Post-Conditions)

    What it's actually doing is matching the alternates but looking at the
    string in reverse order. I want to have look at the string in forward
    order for the alternates, but still keep the reverse order in place
    for the "Description", but not "Change Description" bit

    --
    Colm
    Colm Guest

  3. #3

    Default Re: OR in a regex

    Colm Gallagher wrote: 

    It looks like your problem is that [^.]+ is greedy, it matches as many [^.]
    characters as possible and then backtracks until
    (?:Actors|Pre-Conditions|Post-Conditions) matches. You need to use a
    non-greedy expression that won't backtrack.

    (?<!Change )Description([^.]+?)(?:Actors|Pre-Conditions|Post-Conditions)


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. regex look behind
    By Zach Dennis in forum Ruby
    Replies: 4
    Last Post: October 19th, 10:10 AM
  2. RegEx -- help
    By Lists in forum PHP Development
    Replies: 4
    Last Post: October 10th, 06:51 PM
  3. another how to regex ?
    By Jerry Preston in forum PERL Beginners
    Replies: 4
    Last Post: October 7th, 03:14 PM
  4. A little regex help?
    By Ivan Marsh in forum PERL Miscellaneous
    Replies: 11
    Last Post: July 4th, 06:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139