简体   繁体   中英

Matching all words except those containing numbers

I'm trying to match all words in a line after (After this select) except those containing a number For example in a line I have :

After this select word word1 worldtwo word3 word4 wordfive 502 875 

I want to match only words without numbers the result should be :

word worldtwo wordfive 

The number of words in the line could change

I tried After this select ([a-zA-Z]*) but it matched only one word

http://www.rubular.com/r/MP4eDbTFhZ

I'm using php with regex

The problem is that by including "After this select " in your regular expression, you are anchoring the regular expression to those words. That is, the regex is looking for a word that immediately follows the string "After this select ".

What I would do is to remove the string "After this select " from your input, and then you can use a regex to get all words that contain only alpha characters. You didn't specify what language/flavor of regex you were using, so I'll demonstrate in JavaScript:

var input = 'After this select word word1 worldtwo word3 word4 wordfive 502 875';
var prefix = 'After this select ';
input = input.substring( prefix.length );        // remove prefix
var matches = input.match( /\b[a-z]+\b/ig );

The regex I've used uses word boundary markers ( \\b ) to avoid the usual problems associated with selecting words. Also, instead of using [a-zA-Z] , I just used [az] and added the i flag to make it case insensitive.

EDIT: now that you've updated your question, and I know you're using PHP, I can offer some alternate solutions. If you've got a lot of input, and you're trying to isolate just a certain area for matching, and you don't want the hassle of splitting it, you have a couple of options. Option one is to do one regex to find the big string you're looking for (including the "After this select "), then use groups to get the stuff you want to do the second match in (matching words). Option two is to use PHP's preg_replace_callback function. I'll demonstrate that because it's a bit more flexible (if you need to do replacement, you're right there!):

$input = "After this select word word1 worldtwo word3 word4 wordfive 502 875";
$output = preg_replace_callback(
    '|After this match (.*)|',
    function( $matches ) {
        preg_match_all( "|\\b[a-zA-Z]+\\b|", $matches[1], $words );
        // $words[0] now contains all words consisting only of alpha characters
        return $matches[0];
    }, $input );

And here's how to do it prior to PHP 5.3 (before anonymous functions became available):

function replaceWords( $matches ) {
    preg_match_all( "|\\b[a-zA-Z]+\\b|", $matches[1], $words );
    // $words[0] now contains all words consisting only of alpha characters
    return $matches[0];
}
$input = "After this select word word1 worldtwo word3 word4 wordfive 502 875";
$output = preg_replace_callback(
    "|After this select (.*)|",
    "replaceWords", $input );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM