我有一个文件路径,我正在尝试将最后出现的两个preamble.tex
字符删除为\usepackage{...}
,并通过Powershell完全删除'{}',然后将其转换为变量。
所以,把这个转过来:
/
对此:
.
我已经尝试使用replace cmdlet来解决这个问题,但这似乎更着重于替换所有出现的事件或第一个/最后一个出现的事件,这不是我的问题。任何指导将不胜感激!
编辑:
因此,我有一个excel文件,我正在创建一个powershell脚本,该脚本在每行上的每个循环中使用a,这等于数千个条目。对于每个条目,我想创建一个辅助变量,该变量将使用完整路径,并保存该路径减去最后两个斜杠。这是我正在处理的脚本部分:
xxx-xxx-xx\xxxxxxx\x\{xxxx-xxxxx-xxxx}\xxxxx\xxxxx
$ script.a将以这种格式输出数千个条目: xxx-xxx-xx \ xxxxxxx \ x {xxxx-xxxxx-xxxx} \ xxxxx \ xxxxx 这是预期的。
我希望$ logFileName输出以下内容: xxx-xxx-xx \ xxxxxxx \ x \ xxxx-xxxxx-xxxx.xxxxx.xxxxx
我才刚刚开始了解正则表达式,我相信括号之间的捕获组应该捕获至少一个'\',但是在添加replace + regex之后的测试尝试未显示任何变化。
请告知我是否可以提供更多信息。
谢谢!
答案 0 :(得分:5)
您可以通过两个相当简单的-replace
操作来做到这一点:
{
和}
\
:$str = 'xxx-xxx-xx\xxxxxxx\x\{xxxx-xxxxx-xxxx}\xxxxx\xxxxx'
$str -replace '[{}]' -replace '\\([^\\]*)\\([^\\]*)$','.$1.$2'
第二个模式匹配:
\\ # 1 literal '\'
( # open first capture group
[^\\]* # 0 or more non-'\' characters
) # close first capture group
\\ # 1 literal '\'
( # open second capture group
[^\\]* # 0 or more non-'\' characters
) # close second capture group
$ # end of string
我们用第一个和第二个捕获组值替换,但用.
之前的值代替\
:.$1.$2
如果您使用的是PowerShell Core版本6.1或更高版本,则还可以使用从右到左的-split
:
($str -replace '[{}]' -split '\\',-3) -join '.'
-split '\\',-3
具有与-split '\\',3
相同的效果,但是从右侧而不是左侧分裂。
答案 1 :(得分:3)
在这种情况下,两步方法最简单:
# Input string.
$str = 'xxx-xxx-xx\xxxxxxx\x\{xxxx-xxxxx-xxxx}\xxxxx\xxxxx'
# Get everything before the "{"
$prefix = $str -replace '\{.+'
# Get everything starting with the "{", remove "{ and "}",
# and replace "\" with "."
$suffix = $str.Substring($prefix.Length) -replace '[{}]' -replace '\\', '.'
# Output the combined result (or assign to $logFileName)
$prefix + $suffix
如果您想通过一个-replace
操作(带有嵌套)来完成此操作,事情就会变得更加复杂:
注意:此解决方案需要PowerShell Core (v6.1 +)
$str -replace '(.+)\{(.+)\}(.+)',
{ $_.Groups[1].Value + $_.Groups[2].Value + ($_.Groups[3].Value -replace '\\', '.') }
另请参阅基于优雅的仅PS-Core -split
的解决方案,带有负索引(仅从 end 分离固定数量的令牌)在Mathias R. Jessen's helpful answer中。
答案 2 :(得分:2)
尝试
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = (env) => {
// Configuration in common to both client-side and server-side bundles
const isDevBuild = !(env && env.prod);
const sharedConfig = {
stats: { modules: false },
context: __dirname,
resolve: { extensions: ['.js', '.ts', '.min.js'], modules: [path.join(__dirname, './ClientApp/assets/internal/script/kendo'), 'node_modules'] },
output: {
filename: '[name].js',
publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{ test: /\.ts$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },
{ test: /\.html$/, use: 'html-loader?minimize=false' },
{ test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'], exclude: [path.resolve(__dirname, './ClientApp/assets')] },
{
test: /\.(sc|c)ss(\?|$)/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: './'
}
},
isDevBuild ? 'css-loader' : 'css-loader?minimize', 'sass-loader'
],
include: [
path.resolve(__dirname, './ClientApp/assets')
]
},
{ test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|eot|ttf|otf)$/, use: 'url-loader?limit=25000' }
]
},
plugins: [new CheckerPlugin()]
};
// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig, {
entry: {
'main-client': [
'./ClientApp/boot.browser.ts',
'./ClientApp/assets/internal/style/custom-ui.css',
'./ClientApp/assets/internal/style/typefaces.css',
'./ClientApp/assets/supplemental/style/typefaces.css',
'./ClientApp/assets/styles/custom-project.scss',
'./ClientApp/assets/styles/custom-project-error-page-styles.scss'
]
},
output: { path: path.join(__dirname, clientBundleOutputDir) },
optimization: {
minimizer: [
new UglifyJsPlugin({
chunkFilter: (chunk) => {
if (chunk.name === 'vendor') {
return false;
}
return true;
}
})
]
},
plugins: [
new MiniCssExtractPlugin({
filename: isDevBuild ? '[name].css' : '[name].css',
chunkFilename: isDevBuild ? '[id].css' : '[id].css'
}),
new webpack.ProvidePlugin({ $: 'Jquery', JQuery: 'jquery'}),
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [
new webpack.SourceMapDevToolPlugin({
filename: '[file].map',
moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]')
})
] : [
new webpack.optimize.UglifyJsPlugin(),
new AngularCompilerPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.module.browser#AppModule'),
exclude: ['./**/*.server.ts']
})
])
});
return [clientBundleConfig];
};