简体   繁体   中英

Regular expression for matching specific words in a string

I'm looking for a regular expression to match the beginning of specific words throughout a string. Say I have this:

This is the example string of text.

I would be looking to match each T that starts a word:

his is he example string of ext. 他是外接的他举例字符串。

Any help would be much appreciated.

trying in irb (Ruby):

irb(main):001:0> "This is the example string of text.".scan(/\bt\w+/i)
=> ["This", "the", "text"]

for /\\bt\\w+/i , \\b is the boundary, t is the character t that you want to start with, and \\w+ is alphanumeric or underscore, with 1 or more occurrences. The i is ignore case.

If you want only alphabets and want to match just a t as well, then you can use

irb(main):002:0> "This is the example string of text.".scan(/\bt[a-z]*/i)
=> ["This", "the", "text"]

The [az] means the class of characters from a to z . * means 0 or more occurrences.

I believe that the \\b metacharacter limits a regular expression to "word boundaries", meaning it will let you match "whole words only". By using \\b at the beginning of your regular expression you can match whatever you want, as long as it starts a word.

In your example: \\b[tT]

Similarly, if you'd wish to match the letter T at the end of words you would just place the \\b at the end of the regular expression.

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