Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2> Screw the regex. </h2> <p>Using a regex here seems to be creating a problem for a solution instead of just solving a problem. </p> <p>This task should be 'easy' for even a novice programmer, and the novel idea of regex has polluted our minds!. </p> <pre><code>1: Get Input 2: Trim White Space 3: If this makes sence, trim out any 'bad' characters. 4: Use the "split" utility provided by your language to break it into words 5: Return the first 5 Words. </code></pre> <p>ROCKET SCIENCE. </p> <h3>replies</h3> <blockquote> <p>what do you mean screw the regex? your obviously a VB programmer. Regex is the most efficient way to work with strings. Learn them. </p> </blockquote> <p>No. Php, toyed a bit with ruby, now going manically into perl. </p> <p>There are some thing ( like this case ) where the regex based alternative is computationally and logically exponentially overly complex for the task. </p> <p>I've parse entire php source files with regex, I'm not exactly a novice in their use. </p> <p>But there are many cases, such as this, where you're employing a logging company to prune your rose bush. </p> <p>I could do all steps 2 to 5 with regex of course, but they would be simple and atomic regex, with no weird backtracking syntax or potential for recursive searching. </p> <p>The steps 1 to 5 I list above have a known scope, known range of input, and there's no ambiguity to how it functions. As to your regex, the fact you have to get contributions of others to write something so simple is proving the point. </p> <p>I see somebody marked my post as offensive, I am somewhat unhappy I can't mark this fact as offensive to me. ;) </p> <p>Proof Of Pudding: </p> <pre><code>sub getNames{ my @args = @_; my $text = shift @args; my $num = shift @args; # Trim Whitespace from Head/End $text =~ s/^\s*//; $text =~ s/\s*$//; # Trim Bad Characters (??) $text =~ s/[^a-zA-Z\'\s]//g; # Tokenise By Space my @words = split( /\s+/, $text ); #return 0..n return @words[ 0 .. $num - 1 ]; } ## end sub getNames print join ",", getNames " Hello world this is a good test", 5; &gt;&gt; Hello,world,this,is,a </code></pre> <p>If there is anything ambiguous to anybody how that works, I'll be glad to explain it to them. Noted that I'm still doing it with regexps. Other languages I would have used their native "trim" functions provided where possible. </p> <hr> <h2> Bollocks --> </h2> <p>I first tried this approach. This is your brain on regex. Kids, don't do regex. </p> <hr> <p>This might be a good start </p> <pre><code>/([^\s]+ (\s[^\s]+ (\s[^\s]+ (\s[^\s]+ (\s[^\s]+|) |) |) |) )/ </code></pre> <p>( Linebroken for clarity )</p> <pre><code>/([^\s]+(\s[^\s]+(\s[^\s]+(\s[^\s]+|)|)|))/ </code></pre> <p>( Actual )</p> <p>I've used <code>[^\s]+</code> here instead of your A-Z combo for succintness, but the point is here the nested optional groups</p> <p>ie: </p> <pre><code>(Hello( this( is( example)))) (Hello( this( is( example( two))))) (Hello( this( is( better( example))))) three (Hello( this( is())))) (Hello( this())) (Hello()) </code></pre> <p>( Note: this, while being convoluted, has the benefit that it will match each name into its own group ) </p> <p>If you want readable code: </p> <pre><code> $word = '[^\s]+'; $regex = "/($word(\s$word(\s$word(\s$word(\s$word|)|)|)|)|)/"; </code></pre> <p>( it anchors around the (capture|) mantra of "get this, or get nothing" ) </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload