开发者

Regular Expression to find separate words?

Here's a quickie for your RegEx wizards. I need a regular expression that will find groups of words. Any group of words. For instance,开发者_运维百科 I'd like for it to find the first two words in any sentence.

Example "Hi there, how are you?" - Return would be "hi there"

Example "How are you doing?" - Return would be "How are"


Try this:

^\w+\s+\w+

Explanation: one or more word characters, spaces and more one or more word characters together.


Regular expressions could be used to parse language. Regular expressions are a more natural tool. After gathering the words, use a dictionary to see if they're actually words in a particular language.

The premise is to define a regular expression that will split out %99.9 of possible words, word being a key definition.

I assume C# is going to use a PCRE based on 5.8 Perl.
This is my ascii definition of how to split out words (expanded):

regex = '[\s[:punct:]]* (\w (?: \w | [[:punct:]](?=[\w[:punct:]]) )* )

and unicode (more has to be added/subtracted to suite specific encodings):

regex = '[\s\pP]* ([\pL\pN_-] (?: [\pL\pN_-] | \pP(?=[\pL\pN\pP_-]) )* )'

To find ALL of the words, cat the regex string into a regex (i don't know c#):

@matches =~ /$regex/xg

where /xg are the expanded and global modifiers. Note that there is only capture group 1 in the regex string so the intervening text is not captured.

To find just the FIRST TWO:

@matches =~ /(?:$regex)(?:$regex)/x

Below is a Perl sample. Anyway, play around with it. Cheers!

use strict;
use warnings;

binmode (STDOUT,':utf8');

# Unicode
my $regex = qr/ [\s\pP]* ([\pL\pN_-] (?: [\pL\pN_-] | \pP(?=[\pL\pN\pP_-]) )* ) /x;

# Ascii
# my $regex = qr/ [\s[:punct:]]* (\w (?: \w | [[:punct:]](?=[\w[:punct:]]) )* ) /x;


my $text = q(
  I confirm that sufficient information and detail have been
  reported in this technical report, that it's "scientifically" sound,
  and that appropriate conclusion's have been included
);
print "\n**\n$text\n"; 

my @matches = $text =~ /$regex/g;
print "\nTotal ".scalar(@matches)." words\n",'-'x20,"\n";
for (@matches) {
    print "$_\n";
}

# =======================================

my $junk = q(
Hi, there, A écafé and Horse d'oeuvre 
hasn't? 'n? '? a-b? -'a-? 
);
print "\n\n**\n$junk\n"; 

# First 2 words
@matches = $junk =~ /(?:$regex)(?:$regex)/;
print "\nFirst 2 words\n",'-'x20,"\n";
for (@matches) {
    print "$_\n";
}

# All words
@matches = $junk =~ /$regex/g;
print "\nTotal ".scalar(@matches)." words\n",'-'x20,"\n";
for (@matches) {
    print "$_\n";
}

Output:
**

I confirm that sufficient information and detail have been
reported in this technical report, that it's "scientifically" sound,
and that appropriate conclusion's have been included


Total 25 words
--------------------
I
confirm
that
sufficient
information
and
detail
have
been
reported
in
this
technical
report
that
it's
scientifically
sound
and
that
appropriate
conclusion's
have
been
included


**

Hi, there, A écafé and Horse d'oeuvre
hasn't? 'n? '? a-b? -'a-?

First 2 words
--------------------
Hi
there

Total 11 words
--------------------
Hi
there
A
écafé
and
Horse
d'oeuvre
hasn't
n
a-b
a-


@Rubens Farias:

Per my comment, here's the code I used:

public int startAt = 0;

private void btnGrabWordPairs_Click(object sender, EventArgs e)
    {
        Regex regex = new Regex(@"\b\w+\s+\w+\b"); //Start at word boundary, find one or more word chars, one or more whitespaces, one or more chars, end at word boundary

        if (startAt <= txtTest.Text.Length)
        {
            string match = regex.Match(txtArticle.Text, startAt).ToString();
            MessageBox.Show(match);
            startAt += match.Length; //update the starting position to the end of the last match
        }
     {

Each time the button is clicked it grabs pairs of words quite nicely, proceeding through the text in the txtTest TextBox and finding the pairs sequentially until the end of the string is reached.

@sln: Thanks for the extremely detailed response!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜