What expression to use to capture middle part of a string - vba

Beginner here. 2nd day into VB.
How do I capture the middle part of the string below? What method (Split, Contains, Trim, eg) to use?
Variable string message1:
"background:url(//test.image.com/bao/uploaded/i2/1951211594/TB2m1gjzYlmpuFjSZFl>XXbdQXXa_!!1951211594.jpg_40x40q90.jpg)"
To become:
//test.image.com/bao/uploaded/i2/1951211594/TB2m1gjzYlmpuFjSZFlXXbdQXXa_!!19512>11594.jpg

Dim s
s = Split(strMessage, ":url(")(1)
s = Left(s, Len(s)-2)

If you know the string will always contain "background:url( in the beginning and )" at the end, you can just use Mid$(message1, 17, Len(message1) - 18).

Related

How can I omit the first character for value in vb.net?

I have this var:
Dim number as decimal = -61.52
and I want to delete the first character to be like this:
61.52
I tried to do this. but doesn´t work:
number = Trim(Left(number , Len(number ) - 1))
How can I do this?
You're looking for Math.Abs(), which forces a number to not be negative.
If you really want to treat a number as a string and then do string manipulations on it, you want to use the ToString method on your number. This will let you specify how you want the number formatted.
Dim NumberAsString As String
NumberAsString = number.ToString()
NumberAsString = NumberAsString.SubString(1, NumberAsString.Length - 1)
Note: you shouldn't rely on the default implementation as it will use the current UI culture and not guaranteed to give consistent results if executed under different cultures than when you developed it.

Weird results when splitting strings in VB.NET

I was getting weird results when doing multiple splits on a string, so I decided to make a simple test to figure out what was going on
testString "1234567891011121314151617181920"
If I wanted to get whats between 10 to 20 in Javascript I would do this:
var results = testString.split("10")[1].split("20")[0]
Which would return 111213141516171819
However when I do this in VB I get 111
Split(testString,"10")(1).Split("20")(0)
It seems the 2nd split is only recognizing the first character no matter what I put.
So it's stopping when it finds the next "2" in the string, even "2abc" would have the same outcome even though that string doesn't even exist.
String.Split does not have an overload that takes only a String. The argument is a Char array or String array. Your string is probably being converted to a char array. Explicitly pass a string array like so:
testString.Split(New String() { "10" }, StringSplitOptions.None)
Try wrapping the second split so it's fashioned like the first one, i.e.:
Split( Split(testString,"10")(1), "20" )(0)"
Vb treats the delimiter argument only as a single character.
This is a tricky scenario that I have seen trip people up before, so I think it is worth a little more explanation than the other answers give. In your original format Split(testString,"10")(1).Split("20")(0), you are unknowingly using two DIFFERENT Split functions.
The first Split(testString,"10") is using the Microsoft.VisualBasic.Strings.Split function, which takes String type parameters. http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.split(v=vs.110).aspx
The second .Split("20")(0) is using System.String.Split method, which does not have an overload that takes a String parameter. http://msdn.microsoft.com/en-us/library/System.String.Split(v=vs.110).aspx
So what was happening is:
Split(testString,"10") uses Microsoft.VisualBasic.Strings.Split, which
returns new String() {"123456789", "11121314151617181920"}
(1) means get 1st position of the returned array, which is "11121314151617181920"
"11121314151617181920".Split("20")(0) uses System.String.Split, and attempts to split on string separator "20"
NOTE: The string "20" param gets implicitly converted to a char "2" because the only single parameter overload of String.Split has a signature of Public Function Split (ParamArray separator As Char()) As String(). The ParamArray parameter option allows you to pass a comma delimited list of values into the function, similar to how String.Format works with a dynamic # of replacement values. http://msdn.microsoft.com/en-us/library/538f81ec.aspx
Step 3 code becomes "11121314151617181920".Split(new Char() {CChar("20")})(0), which using literal values is "11121314151617181920".Split(new Char() {"2"c})(0). The result is {"111", "13141516171819", "0"}. Get the 0th position, returns "111".
So to avoid confusion, you should convert your code to use the same version of Split on both sides.
Either of the 2 examples below should work:
Example 1: Using Microsoft.VisualBasic.Strings.Split:
Split( Split(testString,"10")(1), "20" )(0)
Example 2: Using System.String.Split:
testString _
.Split(New String() {"10"}, StringSplitOptions.None)(1) _
.Split(New String() {"20"}, StringSplitOptions.None)(0)

Substring in vb

I am trying to perform a Substring function on a image filename.
The name format is in "images.png".
I tried using Substring it only allow me to indicate the first character till the "n" character to perform the function.
Such that SubString(1,6).
But what I want is to get any character before the ..
For example "images.png":
After the Substring function I should get "images".
You can use LastIndexOf in conjunction with Substring:
myString.Substring(0, myString.LastIndexOf('.'))
Though the Path class has a method that will do this in a strongly typed manner, whether the passed in path has directories or not:
Path.GetFileNameWithoutExtension("images.png")
How about using the Path class.
Path.GetFileNameWithoutExtension("filename.png");
In general for such string manipulations you can use:
mystring.Split("."c)(0)
But specifically for getting a filename without extension, it's best to use this method:
System.IO.Path.GetFileNameWithoutExtension
Dim fileName As String = "images.png"
fileName = IO.Path.GetFileNameWithoutExtension(fileName)
Debug.WriteLine(fileName)
http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx
string s = "images.png";
Console.WriteLine(s.Substring(0, s.IndexOf(".")));

lua variable in pattern match

Im just wondering if it is possible to put a variable in a pattern match in Lua. Like something similar to the following:
var = "hello"
pattern = string.match(datasource, "(var)%s(a%+)")
The reason I need to do this is because the variable "var" will change periodically. (it will be in a loop)
Cheers in advance
Lua doesn't handle string interpolation inside of the quotes. Instead, you'll need to concatenate the parts with the var as a var reference and the rest as quote strings.
"("..var..")%s(a%+)" starts with a "(" as a string literal, concatenates the variable, then finishes off the rest of the string with a string literal.
Use "("..var..")%s(a%+)" instead.
I needed the same thing I think, a variable in a pattern match, but the above solution didn't work for me. I'm posting my solution in case it helps someone, didn't find anything else on the net like it.
I read a ': ' delimited file (name: tel) and want to search by name in the file and have the name and telephone number as answer.
local FileToSearch = h:read'*a' -- Read all the file
var = io.read() -- ask the name
string.gmatch(FileToSearch, ''..var..': '..'%d+') -- search for name, concatenate with number

VB.Net Regular expression

I want to have a string in the following format
"FAG001 FAG002 FAG003"
and want to split it into
"FAG001"
"FAG002"
"FAG003"
using a regular expression. Unfortunately my knowledge of regular expression synatax is limited to say teh least. I have tried things like
Dim result = Regex.Split(npcCodes, "([A-Z]3[0-9]3)").ToList
without luck
No need of regex here, you could use String.Split
Dim result As String() = npcCodes.Split(new Char[]{" "})
But if you really want to use regex :
Dim result = Regex.Split(npcCodes, " ").ToList()
As madgnome has pointed out you don't need regular expressions here if the string is always separated with spaces.
However for your information the error you made was that you need curly braces for numeric quantifiers:
[A-Z]{3}
And instead of Regex.Split you can uses Regex.Matches.
The regular expression to use in the Split method would be really simple:
Dim result = Regex.Split(npcCodes, " ").ToList
As the expression only matches a single character, you can just as well use the regular Split method in the String class:
Dim result = npcCodes.Split(" "C).ToList