在两个字符串正则表达式中查找单词的常见出现次数 [英] Find The Common Occurrences Of Words In Two Strings Regex

查看:29
本文介绍了在两个字符串正则表达式中查找单词的常见出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做同样的事情:找出两个字符串中单词的常见出现次数 但我想要在 VB.net 中.

I'm trying to do the same thing as: Find The Common Occurrences Of Words In Two Strings But I want it in VB.net.

var tester = "a string with a lot of words";

function getMeRepeatedWordsDetails ( sentence ) {
  sentence = sentence + " ";
  var regex = /[^\s]+/g;
  var regex2 = new RegExp ( "(" + tester.match ( regex ).join ( "|" ) + ")\\W", "g" );
  matches = sentence.match ( regex2 );
  var words = {};
  for ( var i = 0; i < matches.length; i++ ) {
    var match = matches [ i ].replace ( /\W/g, "" );
    var w = words [ match ];
    if ( ! w )
      words [ match ] = 1;
    else
      words [ match ]++;
  }   
  return words;
} 

console.log ( getMeRepeatedWordsDetails ( "another string with some words" ) );

这是我目前所拥有的:

    Dim tester As String = "a string with a lot of words"
    sentence = sentence & " "
    Dim regex As New Regex("/[^\s]+/g")
    Dim m As Match = regex.Match(tester)

    'not working
    Dim regex2 As Regex = New Regex("(" + regex.Match(tester).join("|") + ")\\W", "g")

你能帮我把它放到 VB.net 中吗?

Can you help me put this into VB.net?

推荐答案

我想我明白了:

    'string of interest
    Dim sentence As String = "check this string with some words"
    sentence = sentence & " "

    'this is our base string
    Dim BaseStr As String = "base string with a lot of words"

    'use this one liner to create the pattern
    Dim pattern2 As String = "(" & BaseStr.Replace(" ", "|") & ")\W"

    'set up the regex
    Dim regex As New Regex(pattern2)
    Dim mc As MatchCollection
    mc = regex.Matches(sentence)

    'loop to get the results
    For ct As Integer = 0 To mc.Count - 1
        Dim m As Match = mc.Item(ct)
        If m.Success Then
            'returns two groups; get the second without a space
            Debug.Print(m.Groups(1).Captures(0).ToString() & "<<")
        End If
    Next

这篇关于在两个字符串正则表达式中查找单词的常见出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆