简体   繁体   English

只有在c#中的单词开头找到才能替换字母

[英]Replace letter only if found at start of word in c#

I couldn't find a definite answer to this so I am posting a question. 我找不到明确的答案,所以我发帖提问。

I would like to add some bbcode to letters in every word in my string but only when the letter is found at the start of a string. 我想在字符串的每个单词中为字母添加一些bbcode,但只有在字符串开头找到字母时才会这样。 It should add to both upper and lower case letters at start of words. 它应该在单词开头添加大写和小写字母。

For example it would replace: 例如,它将取代:

string s = "Alan ate an apple";

With: 附:

[bbcode]A[/bbcode]lan [bbcode]a[/bbcode]te [bbcode]a[/bbcode]n [bbcode]a[/bbcode]pple

cant' figure out how to achieve this :/ 不能弄清楚如何实现这个目标:/

Search regex: 搜索正则表达式:

(\b[Aa])

Replace by: 替换为:

[bbcode]$1[/bbcode]

RegEx Demo RegEx演示

Here search regex is: (\\b[Aa]) which is matching letter a or A only after a word boundary \\b thus matching only the first letter of a word. 这里搜索正则表达式是: (\\b[Aa])只在字边界\\b后匹配字母aA因此只匹配单词的第一个字母。

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

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