VB.Net find the word in textbox and counting [closed] - regex

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to find the specific word in the textbox, and I want to count it.
For example,
There is TextBox which contains "abababababab" (6 ab)
and there is another textbox wich contains "ab"
And I want to get the result : 6.
How can I do it?

You can do it like this:
Dim text As String = "abababababab"
Dim find As String = "ab"
Dim result As Integer = CInt((text.Length - text.Replace(find, "").Length) / find.Length)

also this way :
Imports System.Text.RegularExpressions
+++++++
Dim txt As String
txt = "abababababab"
Dim count As Integer
count = Regex.Matches(txt, Regex.Escape("ab")).Count()

Replace the matching chars in the first string with empty strings, then subtract from the lenght of the original string the length of the resulting string, finally divide by the lenght of the matching string
Dim test = "abababababab"
Dim result = test.Replace("ab", "")
Dim len = (test.Length - result.Length) / "ab".Length
Console.WriteLine(len)

Related

Regex Match in between Strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I using Regex to extract a pattern which is of the form [a-zA-z][0-9]{8} Ex:K12345678
I need to extract this pattern from a string and this pattern should be matched properly
I tried the below but my testcase if failing for this scenario
This is my Regex /[a-zA-Z][0-9]{8}/g
phonne number:9978434276K12345678:My pattern
For this scenario it is failing.
My Sample Code
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);
The expected output is K12345678.The Regex which I wrote does not handle this.
You can use String.match(Regex)
"9978434276K12345678".match(/[a-zA-Z][0-9]{8}/)
It returns an array of 4 elements: [String coincidence, index: Number, input: String, groups: undefined]
just stay with the element 0: coincidence and 1: index of the match.
and use this just to check that the string matches at least one
/Regex/.test(String)
/[a-zA-Z][0-9]{8}/.test("9978434276K12345678")
It will return true or false
USE expression without quotation marks
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);

How can I get a particular part of a String enclosed between 2 characters using Excel VBA? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to write a macro in excel which can help me get a particular part of a string. My string is as follows :
PAYPAL *VINAYAKAGGRAWAL 4029357733
I need to extract VINAYAKAGGRAWAL from the above string. Any help with vba macro or any excel formula to do this would help.
Thanks in advance.
If your two characters are "*" and " " as in the string above, then you could use this in VBA:
Function GetString(s As String) As String
GetString = Split(Split(s, "*")(1), " ")(0)
End Function
Or use an in-cell formulae on the worksheet:
=TRIM(LEFT(RIGHT(D8,LEN(D8)-FIND("*",D8)),FIND(" ",RIGHT(D8,LEN(D8)-FIND("*",D8)))))
where D8 would have the original string in
Something like:
Sub parser()
Dim s As String, s2 As String, s3 As String
s = "PAYPAL *VINAYAKAGGRAWAL 4029357733"
s2 = Mid(s, InStr(1, s, "*") + 1)
s3 = Left(s2, InStr(1, s2, " "))
MsgBox s3
End Sub
Maybe this:
strClean = Replace("PAYPAL *VINAYAKAGGRAWAL 4029357733", "PAYPAL *", "")
strWanted = Left(strClean, InStr(strClean, " ") - 1)
Debug.Print strWanted
strClean = Split(Replace("PAYPAL *VINAYAKAGGRAWAL 4029357733", "*", ""))(1)
How it works:
First, we get rid of the * character with Replace function - change it to empty string.
Then we split the string by the space with Split function. We don't mention the space delimiter explicitly, because it's a default delimiter for Split.
Finally, we get the second item from the string with (1). Split returns zero-based array, so that first item is (0), second is (1) etc. Since we need second item, we use (1).

Replacement in excel sheet by list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to make bulk replacement using regexp patterns from excel range, see my answer below:
Here is my way to make replacement in Excel range using list from another range based on RegExp:
Sub regexpreplace()
Set Myrange = ActiveSheet.Range("A2:A1000") 'range in which we make replace
Set regrange = ActiveSheet.Range("B2:B6") 'range with RegExp pattern
'in range C1:C6 we have pattern for replace
For Each D In regrange
For Each C In Myrange
Set rgx = CreateObject("VBScript.RegExp")
rgx.IgnoreCase = True
rgx.Pattern = D.Value
rgx.Global = True
C.Value = rgx.Replace(C.Value, D.Offset(0, 1).Value)
Next
Next
End Sub
In this code:
A1:A1000 - range with input values
B1:B6 - list of RegExp patterns
C1:C6 - list of output patterns
Some examples of using script:

VB.net - Strings: Splitting or regex [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
how can I split or use regex for the results of this msgbox to only get:
"maricruz.tinajero"
I thought about removing all new lines so it's only on one line and then using an index number to get the position of the string I want but im still trying to figure that out.
Try this:(str represents your string above)
Dim obj = str.Substring(str.LastIndexOf("\") + 1)
Forget about using Split or Regex for this:
Dim idx As Integer = msg.LastIndexOf("\"c)
Dim user As String = msg.SubString(idx + 1)

How to match a simple number pattern in VBA using RegEx [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
How do I check if a string is either a one digit number OR a two digit number and otherwise return false?
How about:
Function OneOrTwo(i As Integer) As Boolean
Dim objRegEx As Object
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.Pattern = "^\d{1,2}$"
OneOrTwo = objRegEx.Test(i)
End Function
See: http://msdn.microsoft.com/en-us/library/ms974570.aspx
You can also do this using VBA LIKE:
Function OneOrTwo(Digits As Variant) As Boolean
OneOrTwo = Digits Like "#" Or Digits Like "##"
End Function
IF CInt(myNumberString) < 100 Then
MsgBox "String must be either 1 or 2 digits"
Else
Msgbox "Failed"
End IF
Should work for you.
Remou had it right. Here is a RegexContains function so you can use it with all sorts of patterns, not just the one you need now.
Function RegexContains(ByVal find_in As String, _
ByVal find_what As String, _
Optional IgnoreCase As Boolean = False) As Boolean
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.Pattern = find_what
RE.IgnoreCase = IgnoreCase
RE.Global = True
RegexContains = RE.Test(find_in)
End Function
Following the example from Remou, assuming the cell is A1, you'd write:
=RegexContains(A1, "^\d{1,2}$")
Here is what I would try. /d for digit, ? for option 2 digits.
/d/d?
or
/d{1,2}