Seb*_*ian 5 java regex parsing
我需要一个正则表达式来解析属性文件中的键值对,以将它们写入数据库.该应用程序是用java编写的.因为我需要存储有关注释行和空行的信息,所以properties.load对我来说不起作用
关键是所有事情,直到第一次出现未转义的空白或等号(包括转义的空格).值是直到行尾的所有内容,但也可以为空.
它必须符合以下情况:
我尝试了以下正则表达式,但它没有正确分离最后两个案例:
^(\\\s|[^\s=]+)+[\s|=](.*)?$
Run Code Online (Sandbox Code Playgroud)
对于最后两个例子,我使用Rubular:
1. key\
2. key\ key value
Run Code Online (Sandbox Code Playgroud)
代替
1. key\ key\ key
2. value
Run Code Online (Sandbox Code Playgroud)
我也试过这个,但它也不适用于我
在此先感谢您的帮助!
(?<!\\\\)\s
您想在检查空间时使用负向后查找
^((.*?)((?<!\\\\)\\s|=)(.*?)|(\\w+))$
Run Code Online (Sandbox Code Playgroud)
分解它
(.*?) Match everything non greedy up to the next match
((?<!\\\\)\\s|=) Match witespace not preceded by \\
(.*?) Again match everything non greedy up to the next match
|\\w+ Or match strings with no whitespace - this captures case 3 with no value
Run Code Online (Sandbox Code Playgroud)
每个案例都使用此处的工具进行了测试http://www.cis.upenn.edu/~matuszek/General/RegexTester/regex-tester.html
归档时间: |
|
查看次数: |
1989 次 |
最近记录: |