Using regular expressions to count words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pplers
    New Member
    • Apr 2007
    • 60

    Using regular expressions to count words

    I got a txt file and want to count the words. That's the easy part, but then i wanted also to experiment with the different cases.

    For example, replace:
    two or more spaces with only one if they are between words
    a parenthesis between spaces with a space or a space and a parenthesis
    new line with a space
    etc

    I used preg_replace

    [PHP]$a = file_get_conten ts("$some_file" );
    preg_replace('/[0-9A-Za-z][\t\t]{2,}[0-9A-Za-z]/', ' ', $a);
    preg_replace('/[0-9A-Za-z][\n]{1,}[0-9A-Za-z]/', ' ', $a);[/PHP]
    that example should work to replace "hello

    hello" with "hello hello", "hello(space)(s pace)(space)(sp ace)(space)hell o" with "hello hello" right ???


    what about the rest ???

    please help...
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Changed thread title to better describe the problem.

    Try using /\b\w\b/.

    Reference of the various syntactic elements that can appear in regular expressions

    Comment

    Working...