仅当字符串以#开头时,我才能使用什么regEx将字符串拆分成整个单词?
时间:2020-03-06 14:53:23 来源:igfitidea点击:
我已经试过了...
Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item("Post"), "\b\#\b")
但是它正在拆分所有单词,我想要一个以#开头的单词数组
谢谢!
解决方案
由于要在拆分中包含这些单词,因此应使用类似
"\b#\w+\b"
这包括表达式中的实际单词。但是我不确定那不是我们想要的。我们能否提供输入和所需输出的示例?
这似乎有效...
C#
Regex MyRegex = new Regex("\#\w+"); MatchCollection ms = MyRegex.Matches(InputText);
或者vb.net
Dim MyRegex as Regex = new Regex("\#\w+") Dim ms as MatchCollection = MyRegex.Matches(InputText)
给定输入文字...
"" asdfas asdf #asdf asd fas df asd fas #df asd f asdf"
...这将产生...
" #asdf"和" #df"
我会向我们保证,这不会为我们提供字符串Array,但是MatchCollection是可枚举的,因此可能就足够了。
另外,我还要补充一点,我是通过使用Expresso来实现的。这似乎是免费的。这对制作我很差的东西很有帮助。 (即它确实为我逃脱了。)
(如果有人认为我应该删除此伪广告,请发表评论,但我认为这可能会有所帮助:)好时机)
使用PHP的preg_match_all()
:
$text = 'Test #string testing #my test #text.'; preg_match_all('/#[\S]+/', $text, $matches); echo '<pre>'; print_r($matches[0]); echo '</pre>';
输出:
Array ( [0] => #string [1] => #my [2] => #text. )
这是Javascript中的代码:
text = "what #regEx can I use to #split a #string into whole words but only" // what #regEx can I use to #split a #string into whole words but only" text.match(/#\w+/g); // [#regEx,#split,#string]
在Microsoft正则表达式中,请尝试:" \#:w"
在Visual Studio中使用"查找"对我有用。我不知道它将如何转换为Regex类。
编辑:反斜杠被吞下。