RegEx to get path of file, without domain - vb.net

I'm new to regular expressions, and have no clue where to start, it's like a diff language to me. But I need one quick to accomplish a task.
I need to take
http://www.domain.com/folder1/folder2/file_path.txt
and get just
/folder1/folder2/file_path.txt
from it.
Thanks!

construct a URI object from it and one of the properties of it will have what you want.

I think that regex should work:
^http://.*?/(.*)$
(tested with Python)

Since VB.NET is in the tag for this question, I assume you have access at the server side to the Request object:
Dim instance As HttpRequest
Dim value As String
value = instance.Path
This should give you exactly what you asked for.
Edit: On second thought - you could be parsing URLs from some input string... in which case, regex will only help if you have a simple (regular) set of inputs:
Do you know all the possible domains? i.e. are "http://www.ABC.com" and "http://www.DEF.com" the only possible domains?
Then here:
Dim text As String = "http://www.ABC.com/folder1/folder2/file.txt"
Dim pattern As String = "(?:http://www.ABC.com|http://www.DEF.com)(.*)"
Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim g as Group = m.Groups(2) 'Gives the string matched by the capturing parentheses

Supporting more protocols and making the protocol optional too.
((https?|ftp)://)?(.*?)/(.*)

Related

how to evaluate ( math ) a string expression, vb.net

I'm not sure the term I used is correct. but I have a string expression and I want it to be calculated.
this an example
Dim S = "4+4"
dim result = evaluate(S) 'some sort of treatment // that return 8
I'm not sure how this is going to work. I'm familiar with the JS eval function. but it seems that I need to add some sort of a library. and I don't want to do that.
I have found some links about using
dim s = new expression("4+4")
and getting the result with
s.evaluate()
but that require to add another library.
and as I said before I don't want to use any library.
I just want a solution on how to proceed? I have hit a wall.
BTW I'm still a beginner try to answer as simple as you can I would appreciate it.
You can use the DataTable.Compute-"trick":
Dim tbl = new DataTable()
Dim result = Convert.ToDouble(tbl.Compute("4+4", Nothing))
The following arithmetic operators are supported in expressions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
More informations: DataColumn.Expression at Expression Syntax.

How to parse whether a string is a valid US currency value

Originally I was using
Decimal.TryParse(input, New Decimal)
to figure out of user input is valid money. This works for most cases, except I only want to accept money within 2 digits, so "10.001" should not be accepted.
I looked all over SO for this simple and I imagine common issue but could not find an answer.
Mark in the discussion solved this for me. Thanks! Here's the VB.net code for what I was looking for:
Dim regex As Regex = New Regex("[0-9]?[0-9]?(\.[0-9]?[0-9]$)")
Dim match As Match = regex.Match(input)
If Not match.Success Then

Java - Index a String (Substring)

I have this string:
201057&channelTitle=null_JS
I want to be able to cut out the '201057' and make it a new variable. But I don't always know how long the digits will be, so can I somehow use the '&' as a reference?\
myDigits substring(0, position of &)?
Thanks
Sure, you can split the string along the &.
String s = "201057&channelTitle=null_JS";
String[] parts = s.split("&");
String newVar = parts[0];
The expected result here is
parts[0] = "201057";
parts[1] = "channelTitle=null_JS";
In production code you chould check of course the length of the parts array, in case no "&" was present.
Several programming languages also support the useful inverse operation
String s2 = parts.join("&"); // should have same value like s
Alas this one is not part of the Java standard libs, but e.g. Apache Commons Lang features it.
Always read the API first. There is an indexOf method in String that will return you the first index of the character/String you gave it.
You can use myDigits.substring(0, myDigits.indexOf('&');
However, if you want to get all of the arguments in the query separately, then you should use mvw's answer.

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(".")));

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