简体   繁体   English

用于匹配字符串中特定单词的正则表达式

[英]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: 我希望匹配每个开始一个单词的T:

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

Any help would be much appreciated. 任何帮助将非常感激。

trying in irb (Ruby): 尝试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. 对于/\\bt\\w+/i\\b是边界, t是要开始的字符t\\w+是字母数字或下划线,出现1次或更多次。 The i is ignore case. i是无视的情况。

If you want only alphabets and want to match just a t as well, then you can use 如果你只想要字母表并且想要只匹配一个t ,那么你可以使用

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 . [az]表示从az的字符类。 * means 0 or more occurrences. *表示出现0次或更多次。

I believe that the \\b metacharacter limits a regular expression to "word boundaries", meaning it will let you match "whole words only". 我相信\\b元字符将正则表达式限制为“单词边界”,这意味着它会让你匹配“仅整个单词”。 By using \\b at the beginning of your regular expression you can match whatever you want, as long as it starts a word. 通过在正则表达式的开头使用\\b ,只要它开始一个单词,就可以匹配任何你想要的东西。

In your example: \\b[tT] 在您的示例中: \\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. 类似地,如果您希望匹配单词末尾的字母T ,则只需将\\ b放在正则表达式的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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