Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.Net regex matching

Tags:

regex

vb.net

Okay, lets say I have a string "The cat in the hat dog", and I know want to regex match cat and dog from the same string.

So I have something like:

Dim myString As String = "The cat in the hat dog"
Dim regex = New Regex("\bcat\b.*\bdog")
Dim match = regex.Match(myString)
If match.Success Then
    Console.WriteLine(match.Value)
End If

The match.Value returns "cat in the hat dog", which is expected.

But what I really need is to just "cat dog" without the other words in the middle, and I'm stuck.

Thanks for any help!

If it helps, the string I'm trying to parse is something like "Game Name 20_03 Starter Pack r6" and I'm trying to pull "20_03 r6" out as version information. Currently using "\b\d{2}_\d{2}\b.\br\d" as my regex string.

like image 826
Keirathi Avatar asked Jul 21 '14 04:07

Keirathi


People also ask

How do you match in regex?

2.1 Matching a Single Character The fundamental building blocks of a regex are patterns that match a single character. Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" .

What is regex in VB net?

A regular expression is a pattern that could be matched against an input text. The . Net framework provides a regular expression engine that allows such matching. A pattern consists of one or more character literals, operators, or constructs.

How do I return a regex match?

You can retrieve subsequent matches by repeatedly calling the returned Match object's Match. NextMatch method. You can also retrieve all matches in a single method call by calling the Regex. Matches(String, Int32) method.

How does regex match work?

A regex pattern matches a target string. The pattern is composed of a sequence of atoms. An atom is a single point within the regex pattern which it tries to match to the target string. The simplest atom is a literal, but grouping parts of the pattern to match an atom will require using ( ) as metacharacters.


2 Answers

You can parenthesize parts of your regular expression to create groups that capture values:

Dim regex As New Regex("\b(cat)\b.*\b(dog)")

Then use match.Groups(1).Value and match.Groups(2).Value.

like image 113
Ry- Avatar answered Nov 14 '22 23:11

Ry-


Your regex would be,

Dim regex = New Regex("\bcat\b|\bdog\b")

This matches the string cat or dog in the input string.

DEMO

For the second string, your regex would be

\b\d{2}_\d{2}\b|r\d

DEMO

Explanation:

  • \b Matches the word boundary(ie, matches between a word character and a non-word character).
  • \d{2} Matches exactly a two digit number.
  • _ Matches a literal underscore symbol.
  • \d{2} Matches exactly a two digit number.
  • \b Matches the word boundary(ie, matches between a word character and a non-word character).
  • | Logical OR operator usually used to combine two regex patterns.this|that, this or that.
  • r\d Literal r followed by a single digit.
like image 44
Avinash Raj Avatar answered Nov 15 '22 01:11

Avinash Raj