Regex- Get file name after last '\' - regex

I have file name like
C:\fakepath\CI_Logo.jpg.
I need a regex for getting CI_Logo.jpg. Tried with \\[^\\]+$, but didn't workout..
Below is my Javascript Code
var regex="\\[^\\]+$";
var fileGet=$('input.clsFile').val();
var fileName=fileGet.match(regex);
alert(fileName);

Minimalist approach: demo
([\w\d_\.]+\.[\w\d]+)[^\\]

Use this
String oldFileName = "slashed file name";
String[] fileNameWithPath = oldFileName.split("\\\\");
int pathLength = fileNameWithPath.length;
oldFileName = fileNameWithPath[pathLength-1];
in java,
I guess,You can modify this for any other langs.
Edit:
make sure you split with "\\\\" - four slashes

Related

Google Apps Script - find string and return the following characters

The output in the log should be " scripting" because these are the next 10 characters followed by the search criteria "general-purpose". Please visit www.php.net to see what I mean, you will find the search string "general-purpose" on top of www.php.net. I think that I have done some more mistakes in this piece of code, right?
function parse() {
// parse site and store html in response
var response = UrlFetchApp.fetch('www.php.net').getContentText();
// declare search string and new regex object
var str = "/general-purpose/+10-following-charcters";
var regExp = new RegExp("/general-purpose/.{0,10}", "gi");
// find the string "general-purpose" and store the next 10 characters in response
var response = regExp.exec(str[0]);
// expected result in logger output is " scripting"
Logger.log(response);
}
It should be general-purpose(.{0,10}) and not /general-purpose/.{0,10}.
Also regExp.exec(str[0]) should be regExp.exec(str)[1].
This code seems to work fine
var str = UrlFetchApp.fetch('www.php.net').getContentText();
var regExp = new RegExp("general-purpose(.{0,10})", "gi");
var response = regExp.exec(str)[1];
Logger.log(response);

Splitting a string into parts, including quoted strings

So suppose I have this line:
print "Hello world!" out.txt
And I want to split it into:
print
"Hello world!"
out.txt
What would be the regular expression to match these?
Note that there must be a space between each of them. For example, if I had this:
print"Hello world!"out.txt
I would get:
print"Hello
world!"out.txt
The language I'm using is Haxe.
Expanding on Mark Knol's answer, this should work as expected for all your test strings so far:
static function main() {
var command = 'print "Hello to you world!" out.txt';
var regexp:EReg = ~/("[^"]+"|[^\s]+)/g;
var result = [];
var pos = 0;
while (regexp.matchSub(command, pos)) {
result.push(regexp.matched(0));
var match = regexp.matchedPos();
pos = match.pos + match.len;
}
trace(result);
}
Demo: http://try.haxe.org/#5c0B1
EDIT:
As pointed out in comments, if your use case is to split different parts of a command line, then it should be better to have a parser handle it, and not a regex.
These libs might help:
https://github.com/Simn/hxargs
https://github.com/Ohmnivore/HxCLAP
You can use regular expressions in Haxe using the EReg api class:
Demo:
http://try.haxe.org/#76Ea0
class Test {
static function main() {
var command = 'print "Hello world!" out.txt';
var regexp:EReg = ~/\s(?![\w!.]+")/g;
var result = regexp.replace(command, "\n");
js.Browser.alert(result);
}
}
About Haxe regular expressions:
http://haxe.org/manual/std-regex.html
About regular expressions replacement:
http://haxe.org/manual/std-regex-replace.html
EReg class API documentation:
http://api.haxe.org/EReg.html
regex demo
\s(?![\w!.]+"\s)
an example worked for these two case,maybe someone have more better solution

What is the equalient of JavaScript's "s.replace(/[^\w]+/g, '-')" in Dart language?

I am trying to get the following working code in JavaScript also working in Dart.
https://jsfiddle.net/8xyxy8jp/1/
var s = "We live, on the # planet earth";
var results = s.replace(/[^\w]+/g, '-');
document.getElementById("output").innerHTML = results;
Which gives the output
We-live-on-the-planet-earth
I have tried this Dart code
void main() {
print( "We live, on the # planet earth".replaceAll("[^\w]+","-"));
}
But the output becomes the same.
What am I missing here?
If you want replaceAll() to process the argument as regular expression you need to pass a RegExp instance. I usually use r as prefix for the regex string to make it a raw string where not interpolation ($, \, ...) takes place.
main() {
var s = "We live, on the # planet earth";
var result = s.replaceAll(new RegExp(r'[^\w]+'), '-');
print(result);
}
Try it in DartPad

Regex for get the path of file

I have code to display a name of file to a jtable. Here is the code :
StringBuilder nameOfComparedFile = new StringBuilder(); //
if (idLexerSelection != getIDLexer()) {
nameOfComparedFile.append(file.getCanonicalPath()); //
System.out.println(file.getCanonicalPath() + " )");
}
And then, in jtable is displayed like this : D:/Data/File.java
I dont wanna change getCanonicalPath, because on jtable that i Created will be using for next process. My question is : how to get just the name of file using regex
To get just the name:
file.getName()
If you absolutely must use regex:
String filename = file.getCanonicalPath().replaceAll(".*[\\\\/](.*)", "$1");

How to extract youtube video id with Regex.Match

i try to extract video ID from youtube using Regex.Match, for example I have www.youtube.com/watch?v=3lqexxxCoDo and i want to extract only 3lqexxxCoDo.
Dim link_vids As Match = Regex.Match(url_comments.Text, "https://www.youtube.com/watch?v=(.*?)$")
url_v = link_vids.Value.ToString
MessageBox.Show(url_v)
how i can extract video id ?, thanks !
Finally got the solution
Dim Str() As String
Str = url_comments.Text.Split("=")
url_v = Str(1)
Private Function getID(url as String) as String
Try
Dim myMatches As System.Text.RegularExpressions.Match 'Varible to hold the match
Dim MyRegEx As New System.Text.RegularExpressions.Regex("youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)", RegexOptions.IgnoreCase) 'This is where the magic happens/SHOULD work on all normal youtube links including youtu.be
myMatches = MyRegEx.Match(url)
If myMatches.Success = true then
Return myMatches.Groups(1).Value
Else
Return "" 'Didn't match something went wrong
End If
Catch ex As Exception
Return ex.ToString
End Try
End Function
This function will return just the video ID.
you can basically replace "www.youtube.com/watch?v=" with "" using "String.Replace"
MSDN String.Replace
url.Replace("www.youtube.com/watch?v=","")
You can use this expression, in PHP I am using this.
function parseYtId($vid)
{
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $vid, $match)) {
$vid = $match[1];
}
return $vid;
}