I don't know anything about regular expressions.
How can I use a regular expressions to change a file name
I want to change a file name from 'style-dist.css' to style.css
Regex works if you can find a pattern in the strings you are trying to match/change. For example, here you have given just a single string. I am assuming you are trying to remove the '-dist' part from it? If there is some other pattern, let it be known in your question.
It can be done by
new_name = old_name.replace(/-dist/, '');
However, if you don't know regex, and have some time, I recommend getting your feet wet in it. As much as you can. Here are some introductory articles:
http://gnosis.cx/publish/programming/regular_expressions.html
http://www.javascriptkit.com/javatutors/re.shtml
And here is a more full fledged book(online):
http://regex.learncodethehardway.org/book/
If using grunt, you'll need to use the rename property in the gruntfile.
You can find more information in the grunt docs on building file objects dynamically.
You could set up your gruntfile to look something like this.
Note that you'll have to work this into your settings so that everything lines up/etc.
Note that there are no other guardrails in place here...no error checking, etc.
Note that you'll need to work out the globs for src and dest locations
Note that if you are doing this with multiple regex patterns, or your globbing patterns get really complicated, this quickly becomes a nightmare and maybe find a different way to do this.
Code:
copy: {
main: {
files: [
{
expand: true,
cwd: '<whatever your cwd is>',
src: ['<glob for your -dist.js file location>'],
dest: '<glob for a PATH..a PATH>',
rename: function(dest, src) {
// receives the dest PATH and src and then
// takes the dest path, appends the modified src using the regex
return dest + src.replace(/-dist/, '');
}
}
Related
At work, I often have to change the paths of files. For example, there is:
qwerty123/aasdad/index.js
qwerty321/ewqewe/script.js
I need to replace it with
js/index.js
There are also pictures and css files. They are always in the img and css folders. So I couldn't get it right. My mind was not enough =( Changing paths for this small task takes about 10 minutes, it's a pity to spend them on this.
Tried this:
src="(.*)" ".*/(.+)\.jpg"
but it also cuts out the file name, but I need it.
Find: [^\s]*\b(\/.*\.js\b)
Replace: js$1
If your paths have spaces in them the find regex won't work as the demo shows!
See Rege101 demo
pathToBins=$1
bins="${pathToBins}contigs.fa.metabat-bins-*"
for fileName in $bins
do
echo $fileName
done
My goal is to attach a path to my file name. I can iterate over a folder and get the file name when I don't attach the path. My challenge is when I add the path echo fileName my regular expression no longer works and I get "/home/erikrasmussen/Desktop/Script/realLargeMetaBatBinscontigs.fa.metabat-bins-*" where the regular expression '*' is treated like a string. How can I get the path and also the full file name while iterating over a folder of files?
Although I don't really know how your files are arranged on your hard drive, a casual glance at "/home/erikrasmussen/Desktop/Script/realLargeMetaBatBinscontigs.fa.metabat-bins-*" suggests that it is missing a / before contigs. If that is the case, then you should change your definition of bins to:
bins="${pathToBins}/contigs.fa.metabat-bins-*"
However, it is much more robust to use bash arrays instead of relying on filenames to not include whitespace and metacharacters. So I would suggest:
bins=(${pathToBins}/contigs.fa.metabat-bins-*)
for fileName in "${bins[#]}"
do
echo "$fileName"
done
Bash normally does not expand a pattern which doesn't match any file, so in that case you will see the original pattern. If you use the array formulation above, you could set the bash option nullglob, which will cause the unmatched pattern to vanish instead, leaving an empty array.
I would like to get assets from a parent directory in a joinTo like this :
exports.config =
paths:
public: './../public'
compass: './config.rb'
watched: ['app']
files:
javascripts:
joinTo:
'/js/master.js':/^(../../../directory/target|app[\/\\]public)/
My problem come from this ../../../directory/target to up one or more directory.
But it seem impossible, because BrunchJS use Regex to find directory.
There is a way to do what i wish?
Thanks
Not sure I completely understand your question or description of what's going wrong. Or even quite what you're trying to match with that regex - is app a sibling of target, or local to cwd?
But you don't need to use a single regex. You may find it much easier to use a glob pattern or an array of globs/regexes/paths such as
files:
javascripts:
joinTo:
'js/master.js': [
'../../../directory/target/**/*',
'app/public/**/*'
]
Take a look at the Brunch joinTo documentation and anymatch documentation.
You also need to watch out for that leading slash in your target file, which may get interpreted as an absolute path.
I'm currently running a regular expression in a node file that's designed to copy my custom fontello icon font files across to the public directory. To ensure I'm not copying irrelevant files I'm using the following:
var match = new RegExp(/\.(woff|svg|ttf|eot)/g);
if (match.test(fileName)) {
// Do something
}
As I cycle through the fonts available, the only two that are being matched are
app.svg
app.eot
The app.ttf and app.woff files are not matching the expression.
I have tried out the expression over at http://www.regexr.com/ and it appears to work for my purposes. Keep in mind that I don't require much more stringent testing than this as there is only a handful of files in that directory.
If anybody can give me some guidance I would be most appreciative.
Your regex is correct.Tried it.
See
I'm trying to load multiple .txt files in R, from different folders.
I have problems writing the path and pattern using regular expressions.
My path has this structure:
'/Users/folderA/folderB/folderC/folderD/01_01_2012/folderE/file.txt'
So, the path is almost the same, except that the folder with the date name always changes.
I have tried to load it like this:
filesToProcess <- list.files(path = "/Users/folderA/folderB/folderC/folderD/",
pattern = "*_*_*/folderE/*.txt")
But this doesn't seem to work.
Could someone please help me writing down this with regular expressions?
Thanks a lot!
The key here is to use argument recursive=TRUE so that you can search inside the folders that are in the original directory:
filesToProcess <- list.files(path = "/Users/folderA/folderB/folderC/folderD",
pattern = "txt", recursive = TRUE, full.names = TRUE)
The pattern has to correspond to the name of the files, it can't refer to the name of the folders (see ?list.files). That's why you need a second step where you have to narrow down to the specific folders you wanted. Note the use of argument full.names=TRUEin the previous call that allow us to keep the path of each file (NB: you also have to drop the final / of the path argument or else it ends up doubled in our output and leads to an error when you'll try to upload the files).
filesToProcess[grep("folderE", filesToProcess)]
A final note:
Your regular expression was flawed anyway: * means
The preceding item will be matched zero or more times.
What you wanted was .: see ?regexp
The period . matches any single character.
Although the subject refers to regular expressions it seems from the example that you really want to use globs. In that case try:
Sys.glob("/Users/folderA/folderB/folderC/folderD/*_*_*/folderE/*.txt")