Split a string by spaces — preserving quoted substrings — in Python/Jython
2012-05-09 14:52
447 查看
最近在解析命令行参数的时候碰到python字符分割的问题,python中字符串分割默认都是在空格,但是我的参数中可能某个参数带有空格符,同时有双引号包围。
最近的python中引入了支持正则分割的shlex模块,他能很好的处理空格符的问题。如下:
程序需要支持所有的Oracle11g的weblogic环境中运行,也就是
WLST online is supported on WebLogic Server 11g Release 1 (10.3.1, 10.3.2, 10.3.3, 10.3.5, and
所支撑的jython版本是 Jython 2.2.1.
但是 Jython 只有在2.3版本才引入[/code]
尝试半天只能使用最古老的方法,正则表达式来解决;
jvmArgsList = [p for p in re.split("( |\\\".*?\\\"|'.*?')", jvmOptions) if p.strip()]
该方法支持命令行参数中含有被单引号或双引号包围的空格符。
比如:[/code]
最近的python中引入了支持正则分割的shlex模块,他能很好的处理空格符的问题。如下:
>>> import shlex >>> shlex.split('this is "a test"') ['this', 'is', 'a test']但是我需要我写jyhon程序需要通过在Weblogic的脚本工具中运行即WLST.
程序需要支持所有的Oracle11g的weblogic环境中运行,也就是
WLST online is supported on WebLogic Server 11g Release 1 (10.3.1, 10.3.2, 10.3.3, 10.3.5, and
10.3.6), 10.3, 10.0, 9.x, 8.1, and 7.0.
所支撑的jython版本是 Jython 2.2.1.
但是 Jython 只有在2.3版本才引入[/code]
shlex.split语法,所以原理上我不能使用这一函数。
尝试半天只能使用最古老的方法,正则表达式来解决;
jvmArgsList = [p for p in re.split("( |\\\".*?\\\"|'.*?')", jvmOptions) if p.strip()]
该方法支持命令行参数中含有被单引号或双引号包围的空格符。
比如:[/code]
jvmOptions ="-DUseSunHttpHandler=true -Dtest.dir='C:\SAS Test'" 程序能把各参数分割开来。
相关文章推荐
- Python: keep strings in double quote as one word when split string to words
- how to split string in c++
- FAILED: ParseException line mismatched input '<EOF>' expecting StringLiteral near 'by' in
- IOS 字符串中去除特殊符号 stringByTrimmingCharactersInSet
- 467. Unique Substrings in Wraparound String
- 151. Reverse Words in a String Leetcode Python
- Mac OSX 正確地同時安裝 Python 2.7 和 Python3 By Candy Tsai In Come on~ Python 11-29-2015 2 Min read 8 comme
- Python modify string in place
- IOS 字符串中去除特殊符号 stringByTrimmingCharactersInSet 应该用于账号登录等
- 第十一周:( LeetCode567) Permutation in String(c++ && python)
- Implementing a Principal Component Analysis (PCA) in Python step by step
- 【LeetCode with Python】 Reverse Words in a String
- leetcode 467 c语言. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String
- Step-by-step for Storage Spaces Tiering in Windows Server 2012 R2
- python 字符串split (string split)
- 【CodeForces】914 F. Substrings in a String bitset
- 将字符串转成单个的字符Split a string to characters in Bash{转}
- IOS 字符串中去除特殊符号 stringByTrimmingCharactersInSet 应该用于账号登录等
- Reverse words in a string by java