I could have sworn i have done this before, but.... no go.
I am trying to copy the file name from each line of the sample data below to the beginning of the line. However, when when i add parathensis to the expression to capture the file name it is deleted. I have tried several variations.
Regex Expresion
[^\\/:*?<>]+$
The expression successfully captures the file names on each line.
Sample Data
c:\Dir1\dir2\Samplefile.txt
c:\Dir1\dir2\dir3\Sample file.txt
c:\Dir1\dir2\Samplefile
c:\Dir1\dir2\dir3\Sample file
c:\Dir1\wp_movfiles_20160911024934.ini
c:\Dir1\\dir2\wp_movfiles_20160911055222.ini
desire results
Samplefile.txt c:\Dir1\dir2\Samplefile.txt
Sample file.txt c:\Dir1\dir2\dir3\Sample file.txt
Samplefile c:\Dir1\dir2\Samplefile
Sample file c:\Dir1\dir2\dir3\Sample file
wp_movfiles_20160911024934.ini c:\Dir1\wp_movfiles_20160911024934.ini
wp_movfiles_20160911055222.ini c:\Dir1\\dir2\wp_movfiles_20160911055222.ini
any assitance is greatly appricated. thank you.
I think you can get away with using this regex:
.*\\(.*)$
This will greedily consume everything in the file path from left to right, until hitting the final backslash. Then it will stop, and capture everything which comes after that final backslash, which should be the file name.
Demo here:
Regex101
Related
I have txt file contaning one url per row each url as:
://url/files.php?file=parent/children/file.pdf
://url/files.php?file=parent/children2/childrenofchildren2/file2.txt
......etc
I need help to cut everythink before last / in a row. That is what I used in notepad++ regex mode (it doesnt work):
^.+[/](.*)$
To get:
file.pdf
file2.txt
But I am open to all waysof solving.
Replace your line from left including / by nothing:
sed 's/.*\///' file
or
sed 's|.*/||' file
Output:
file.pdf
file2.txt
This solution may be more complicated than it needs to be, but it works!
A purely regex-based approach could be as follows:
(([^\/])*)((\n)|($))/g
Basically, it matches any number of non-newline and non \ characters (([^\/])*) and then stops when it either encounters a new line \nor the end of the sequence $. The global /g is also set, to allow it to match more than one instance!
I hope this helped!
I have a netlist generated from schematic. This netlist includes power pins. Iam trying to write a perl script to remove power pins from netlist.
As part of this i have to search for a string that matches the pattern shown below:
", );"
I have used the following code and it is not working
$line =~ s/,\s+\);//g
I have observed that pattern end with comma are matched but pattern starting with comma or pattern with comma in middle are not matched.
Any suggestions on how to get this work
You need to use this instead:
s/,\s*\);//
You should be defensive and be able to handle no whitespace between the , and the ). You have to escape the ). See perldoc perlre for more info.
Thank you every one. I have found the problem. The problem was that the pattern to be recognized is split in to two different lines. The "," is in one line followed by ");" in next line. At first, iam removing the new line character and assumed that the next line will get appended to the current line, which is not happening. Hence, the pattern matching did not work.
To resolve this, i have to read the file once again and then replace the pattern.
I'd like to get a regex like so:
"for each /xxx.png go to the inmediately next /yyy.eps, and change it to /xxx.eps"
If possible, how could I do it with regex?
I'm working in a CSV file and using Notepad++.
Many thanks!
EDIT
Hoping this helps to clarify, a better example would be:
line 1: "landscape123.png","IwantToBeNamedlandscape123.eps"
line 2: "picture123.png","IdLikeToBeNamedpicture123.eps"
How can I take the pngs filenames and replace the next .eps filenames with them? Each time, both file types are on the same line.
Find:
^"(.*)\.png".*$
Replace with:
"\1.png","\1.eps"
This says: "Find lines that contain exactly: ", a filename (and capture the filename), .png", and then whatever; and then replace them with "\1.png","\1.eps"", where \1 is a backreference that contains the filename.
Make sure you have ". matches newline" unchecked.
I am trying to name a folder based on a file name, using regex in powershell.
The name looks something like this tester2-2013-14-10.txt.
With that file name being said, I want to create a folder named tester2. It will do this for all files in a directory. Basically the regex needs to read up until the first '-', then ignore all other characters after that. It
All regular expressions that I am trying have not worked to do such a things.
Here is the regex I am trying to use (\w+)\.txt.
And here it is when I try and implement it:
Get-ChildItem | Where{$_.Name -match '(\w+)\.txt'} | ForEach-Object{
md $matches[1]}
Along with other variations of this type of thing.
It appears that when I run a file name against this regex, it is using the end of the string. Such as in the case of tester2-2013-14-10.txt. The folder name that is being created is called 10 instead of Tester2.
Appreciate the help!
Try following regex
^(\w+)-.*\.txt
Refer to regex101 demo for validation and explanation.
Short Description
^ matches start of the line or string. It will ensure that match should be at the starting of a line.
Hence ^(\w+) will match the first word in the file name. Then it has to be followed by - and any characters until .txt
What's wrong with your regex?
(\w+)\.txt will match a word (\w+) immediately followed by .txt.
Hence you are getting last word instead of the first word.
How can I extract the filename without extention from the following file path:
D:\Projects\Extract\downtown - second.pdf
The following regular expression gives me the filename with extention: [^\\]*$
e.g. downtown - second.pdf
The following regular expression gives me the filename without extention: (.+)(?=(\.))
e.g. D:\Projects\Extract\downtown - second
I'm struggling to combine the two into one regular expression to give me the results I want: downtown - second
I suspect that your 2nd regex would not give you the output you have shown. It will give you the complete string till the first period (.).
To get just the file name without extension, you can use this regex: -
[^\\]*(?=[.][a-zA-Z]+$)
I have just replaced (.+) in your 2nd regex with the [^\\]* from your first regex, and added pattern to match pdf till the end.
Now this pattern will match 0 or more repetition of any character but backslash(\), followed by a . and then 1 or more repetition of alphabets making up extension.
I made up this one, which allows to capture most of the possibilities:
/[^\\\/]+(?=\.[\w]+$)|[^\\\/]+$/
/path/to/file
/path/to/file.txt
/path.with/dots.to/file.txt
/path/to/file.with.dots.txt
file.txt
C:\path\to\file.txt
and so on...
I captured file from /path/to/file.pdf by using following regex:
[^/]*(?=\.[^.]+($|\?))
Hope this helps you
I had to use an extra backslash before the first ']' to make this work
[^\\\]*(?=[.][a-zA-Z]+$)
I use this pattern
[^\/]+[.+\.].*$ for / path separator
[^\\]+[.+\.].*$ for \ path separator
hich matches the filename at the end of the string without worrying about characters. There is one exception that if the path for some reason has a folder with a period in it this will get upset. Linux hidden directories that are preceded with a . like .rvm are unaffected.
Hope this helps.
http://rubular.com/r/LNrI4inMU1