标题: [文件操作] 【已解决】求助批处理替换特殊字符串小括号的问题? [打印本页]
作者: jackelijie135 时间: 2009-3-16 19:20 标题: 【已解决】求助批处理替换特殊字符串小括号的问题?
关于替换特殊字符串的疑问!
例如文本文件test.txt内容如下:
"you are my super star!"
"you are :my super star!"
"you are my super star!"
如何将文本文件内容变成
you are my(not you) super star!
you are my(not you) super star!
you are my(not you) super star!- @echo off
- echo "you are my super star!">test.txt
- echo "you are :my super star!">>test.txt
- echo "you are my super star!">>test.txt
- echo.
- @echo off
- ::去引号
- setlocal EnableDelayedExpansion
- for /f "tokens=* delims=" %%i in (test.txt) do (
- set var=%%i
- set var=!var:"=!
- echo !var!>>ls.txt
-
- )
- pause
- echo.
- @echo off
- ::去冒号
- setlocal EnableDelayedExpansion
- for /f "tokens=* delims=" %%i in (ls.txt) do (
- set var=%%i
- set var=!var::=!
- echo !var!>>ls1.txt
-
- )
- pause
- echo.
- ::把my替换为my (not you)
- @echo off
- setlocal EnableDelayedExpansion
- for /f "tokens=* delims=" %%i in (ls1.txt) do (
- set var=%%i
- set var=!var:my=my (not you)!
- echo !var!
-
- )
- echo.
- pause
复制代码
前两个都可以正常执行,但是第三个不能执行;
我估计应该是do后面的左括号遇到not you后面的右括号就终止了;
请高手指点怎么来实现被替换的字符为包含有小括号的情况!
当然可能还会有其他特殊字符不能按照正常的替换方式替换,请给出解决方案!谢谢
呵呵,后来我想用变量来替换- @echo off
- setlocal EnableDelayedExpansion
- set /p temp=my (not you)
- for /f "tokens=* delims=" %%i in (ls1.txt) do (
- set var=%%i
- set var=!var:my=!temp!!
- ::set var=!var:my=%temp%! 貌似这样也不可以!
- echo !var!
-
- )
- echo.
- pause
复制代码
希望各位高手指点O(∩_∩)O哈!
pause
[ 本帖最后由 jackelijie135 于 2009-3-17 20:37 编辑 ]
作者: zjw767676 时间: 2009-3-16 21:58
- @echo off
- echo "you are my super star!">test.txt
- echo "you are :my super star!">>test.txt
- echo "you are my super star!">>test.txt
- echo.
- @echo off
- ::去引号
- setlocal EnableDelayedExpansion
- for /f "tokens=* delims=" %%i in (test.txt) do (
- set var=%%i
- set var=!var:"=!
- echo !var!>>ls.txt
-
- )
- pause
- echo.
- @echo off
- ::去冒号
- setlocal EnableDelayedExpansion
- for /f "tokens=* delims=" %%i in (ls.txt) do (
- set var=%%i
- set var=!var::=!
- echo !var!>>ls1.txt
-
- )
- pause
- echo.
- ::把my替换为my (not you)
- @echo off
- setlocal EnableDelayedExpansion
- for /f "tokens=* delims=" %%i in (ls1.txt) do (
- set var=%%i
- set var=!var:my=my ^(not you^)!
- echo !var!
-
- )
- echo.
- pause
复制代码
把set var=!var:my=my (not you)!
改成这样试试
set var=!var:my=my ^(not you^)!
作者: jackelijie135 时间: 2009-3-16 22:30 标题: 回复 2楼 的帖子
O(∩_∩)O哈哈~,谢谢你啊,可以啦
作者: zqz0012005 时间: 2009-3-17 12:34
原理参见:
复合语句内部子语句的括号问题
http://bbs.verybat.org/viewthread.php?tid=10741&fromuid=37
作者: jackelijie135 时间: 2009-3-17 20:39 标题: 回复 4楼 的帖子
谢谢了,请问如何给别人加分啊,为了表示谢意,O(∩_∩)O~
作者: Batcher 时间: 2009-3-17 20:43 标题: 回复 5楼 的帖子
列兵级别暂时无法加分,请参考:http://bbs.bathome.net/thread-1869-1-1.html
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |