Get text between \ and \PARTS from file name string vb.net - vb.net

I need to obtain a value of everything between \ & \PARTS
Example:
`XamoutOFdirectorys\THIS IS WHAT I WANT\PARTS`
resulting in the text, THIS IS WHAT I WANT
in dir1\dir2\THIS IS WHAT I WANT\PARTS
I want the text THIS IS WHAT I WANT and not dir2\THIS IS WHAT I WANT
How can I achive that?
The reason is that I need to know what the files name is that is found before the PARTS directory, regardless of howmany directorys are found before and after...
the closest i can achive is ...
Dim text As String = "fat\dir1\dir2\PARTS\bat"
Dim str As String = text.Split("\"c, "\PARTS"c)(1)
MsgBox(str)

Dim str As String = "fat\dir1\dir2\PARTS\bat"
Dim dir As Match = Regex.Match(str, "\\([A-Za-z0-9\-]+)\\PARTS")
MsgBox(dir.Groups(1).ToString)

Try this
It should return an array with each text between "\". Then you can search for the text "PARTS" and take the previous index.
Split -> [dir1, dir2, your text, PARTS]
index of PARTS = 3
index of your text = 2
I don't really know vb.net but that's how I would do it with any other language.

If your path variable type is String. You can find "\PARTS" in path, get the start index A in path. Then find another index B of last "\" before A. Using substring function to get what you want between range [B, A] in path variable:
Dim str As String = "fat\dir1\XamoutOFdirectorys\_THIS IS WHAT I WANT\PARTS\bat"
Dim beginIdx, endIdx As Integer
endIdx = str.LastIndexOf("\PARTS") - 1
beginIdx = str.LastIndexOf("\", endIdx - 1) + 1
Dim result = str.Substring(beginIdx, endIdx - beginIdx + 1)
By the way, there are more elegant methods such as regular expression. But I really advice you should read MSDN about String, dirt your hand, get the solution by yourself. There are also many solution about "split path" in Stack Overflow that you can change them after you understand the solution. Best regards.
regular expression like this:
Dim str As String = "fat\dir1\XamoutOFdirectorys\_THIS IS REALLY WHAT YOU WANT.&%+\PARTS"
Dim dir As Match = Regex.Match(str, "\\([^\\]+)\\PARTS")
MsgBox(dir.Groups(1).ToString)
Which can work in real world and support all possible path versions(tested in windows system).
And the state machine illustration of my regular expression
\\([^\\]+)\\PARTS
[Debuggex Demo](https://www.debuggex.com/r/rYms5zrhWCby_FdP

Related

How to read a string from starting index

Dim text as string = "Visual Basic"
output: "Basic"
I need to read a string and print it but not starting at the very first index. The text may vary so I want to know if there is a way to read it.
You can use the IndexOf method to find the first space, then the SubString method to get the part of the string after the space:
Dim idx As Integer = text.IndexOf(" "C);
Dim output As String = text.SubString(idx + 1);
(This even works if there would be no space at all in the string. Then idx will be -1 (the value meaning "not found"), so idx + 1 will be zero and output will be the whole string.)

What method can I use to retrieve a string using two character indexes?

Just like the title says; I want to use something like Mid(stringName, startIndex,[integerLength]), but instead of the third argument taking a string length, I want it to take the end character index. So in an example like this
alphabet = "ABCDEFG"
partial = *method I want to use*(alphabet, 2, 4) 'partial would equal "BC"
(Forgive me if my index numbers are off, but I hope you get my point.)
Does something like this exist in VB.NET?
You'll want to use String.Substring
http://msdn.microsoft.com/en-us/library/aka44szs.aspx#Y0
dim alphabet as string = "ABCDEFG"
'partial is a reserved word!
'1,2 is the correct parameters to get 'BC'
dim partialString as string = alphabet.Substring(1, 2) 'partial would equal "BC"
Edit - Oooooh you want to do StartIndex,StopIndex not StartIndex,Length. Just apply a bit of math.
dim startIndex as integer = 1
dim stopIndex as integer = 3
'partial would equal "BC"
dim partialString as string = _
alphabet.Substring(startIndex , stopIndex-startIndex )
I'd wrap that in an extension method on string, giving it a new name of course.
Just use Mid, the math for the length is quite easy (length = endIndex - startIndex):
part = Mid(alphabet, 2, 4-2)
You could also achieve the same thing with Substring (which uses 0 based indexes rather than 1 based):
part = alphabet.Substring(1, 3-1);
targetstring=alphabet.Substring(2,4)
The above one should work..

splitting a string to access integer within it

i have a string "<PinX F='53mm'></PinX>", I want to access the 53 within the string and do some addition to it and then add the answer back into that string. I've been thinking about this and wasn't sure whether this can be done with regular expression or not? Can anybody help me out.
thanks
Yes, you can use a regular expression. This will get the digits, parse them to a number, add one to it, and put it back in the string (that is, the result is actually a new string as strings are immutable).
string s = Regex.Replace(
input,
#"(\d+)",
m => (Int32.Parse(m.Groups[1].Value) + 1).ToString()
);
Take a look at the HTML Agility Pack.
A regular expression looks like a good fit for this particular problem:
\d+
Will match one or more digits.
Int32.Parse(Regex.Match("<PinX F='53mm'></PinX>", #"\d+").Value)
Will return 53.
In this single case yes. "'(.*?)' then access the first group, but if this is part of a larger xml regular expressions should not be used. You should utilize the xml parser build into .net find the attribute with xsd and get the value.
Alternatively, here's a small routine...
' Set testing string
Dim s As String = "<PinX F='53mm'></PinX>"
' find first occurence of CHAR ( ' )
Dim a As Integer = s.IndexOf("'")
' find last occurence of CHAR ( ' )
Dim b As Integer = s.LastIndexOf("'")
' get substring "53mm" from string
Dim substring As String = s.Substring(a, b - a)
' get integer values from substring
Dim length As Integer = substring.Length
Dim c As Char = Nothing
Dim result As String = Nothing
For i = 1 To length - 1
c = substring.Chars(i)
If IsNumeric(c) Then
result = result & c
End If
Next
Console.WriteLine(Int32.Parse(result))
Console.ReadLine()

Need Assistance on getting LINK ID using Regular Expressions?

I got this url for example "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
I want to get the last digit numbers and the rest could be anything.
I need a format like this "http://www.yellowpages.com/anything.... lid=randomdigitnumbers" or as long as i get those numbers.
My knowledge is very poor in this regex thing so please guys help me.
the following did not work
Dim r As New System.Text.RegularExpressions.Regex("http://www.yellowpages.com/.*lid=d*", RegexOptions.IgnoreCase)
Dim m As Match = r.Match(txt)
If (m.Success) Then
Dim int1 = m.Groups(1)
MsgBox("(" + int1.ToString() + ")" + "")
End If
thank you in advance
Using Regular Expressions for this is a bit of overkill, IMO.
You could accomplish the same thing using string functions:
Dim url As String = "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
Dim queryString As String = url.SubString(url.IndexOf("?"), url.Length - url.IndexOF("?"))
Dim nameValuePairs As String() = queryString.Split("=")
Dim lid As String = nameValuePairs(1)
This is off the top of my head, so you may need to tweak it a bit. The basic concept is to the portion of the URL after the ? (the query string), and then split it on the = sign, taking the second element of the resulting array (the value).
Also, if the query string has more than one name value pair, they'll be separated by &, so you'll need to split on the ampersand (&) first, then the equal signs.
Just find lid= and get everything after that:
Dim url As String = "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
Dim lidIndex As Integer = url.IndexOf("lid=") + "lid=".Length
Dim lid As Integer = url.Substring(lidIndex)

VB: Need help splitting a string into 3 variables

I need to split one variable into 3 variables. For example I have a variable called Data and it contains AAA00000001. I need to have them be split between the "AAA", "0000000", and "1". I looked at the Split() function but didn't get a clear example for this situation. The reason I need to do this is because I want to store these 3 variables into fields for a MySQL database.
Are the three subvariables always of the same length?
If so, you can use Substrings:
Dim substring1 As String = Data.Substring(0, 3)
Dim substring2 As String = Data.Substring(3, 7)
Dim substring3 As String = Data.Substring(10, 1)
Assuming the string is ALWAYS the EXACT same length and need to be split at the SAME place, you can use Substring().
dim s as String = "AAA00000001"
dim s1 as String = s.Substring(0, 3)
dim s2 as String = s.Substring(3, 7)
dim s3 as String = s.Substring(10)
If they're not always the same length, you're probably going to need to use Regular Expressions.
Split will break your string apart based on a character, or group of. It's not appropriate here, that is unless you're always splitting on 0000000, which I doubt you are.
If you know that the first 3 characters will always be your first group, second 7 your next, and last character, your final group, you could do something like this.
This uses the Substring function, e.g.
Dim yourString as String = "AAA00000001"
Dim c1 As String = yourString.Substring(0, 3)
Dim c2 As String = yourString.Substring(3, 7)
Dim c3 As String = yourString.Substring(10, 1)