java 正则表达式匹配管道描述的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7959929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex To Match A Pipe-Delineated File
提问by Jason
I need help with a regex to check if a line matches a line of pipe delineated data. The data will end with a pipe, and is not quoted. Some fields will be empty.
我需要正则表达式的帮助来检查一行是否与管道描绘的数据行匹配。数据将以管道结尾,并且没有引用。某些字段将为空。
Here's what I'm trying to use:
这是我正在尝试使用的内容:
Pattern dataPattern = Pattern.compile("(.+)\|^");
Here is a sample line of data:
这是一个示例数据行:
GJ 3486|||121.10766667|-83.23302778|295.84892861999998|-24.832649669999999||-0.48399999999999999||.371|2MASS J08042586-8313589|8.9700000000000006|8.3539999999999992|8.1110000000000007||2MASS||
Since I only wanted to see if the line matched the pattern, I thought the one I came up with would look for "blah blah blah |". Apparently not... can anyone help me out?
由于我只想查看该行是否与模式匹配,因此我认为我想出的那个会寻找“blah blah blah |”。显然不是……有人可以帮我吗?
Jason
杰森
回答by FailedDev
^(.*?\|)*$
Try this instead.
试试这个。
"
^ # Assert position at the beginning of the string
( # Match the regular expression below and capture its match into backreference number 1
. # Match any single character that is not a line break character
*? # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\| # Match the character “|” literally
)* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
Some problems with your regex :
你的正则表达式的一些问题:
- Fist it is not repeating, you should repeat the pattern since you have many columns.
- You match something and then you match tne start of the string. Not possible, this will never match.
- You always want a character to match but you said there could be empty columns. Instead use * quantifier.
- 拳头不重复,您应该重复该模式,因为您有很多列。
- 您匹配某些内容,然后匹配字符串的开头。不可能,这永远不会匹配。
- 您总是希望匹配一个字符,但您说可能会有空列。而是使用 * 量词。
回答by Vlad
Your regex is wrong it should be:
你的正则表达式错了,应该是:
Pattern dataPattern = Pattern.compile("(.+)\|$");
回答by thejh
How about this?
这个怎么样?
str.length() > 1 && str.charAt(str.length()-1) == '|'
Is probably much faster.
可能要快得多。
回答by Fred
Pattern dataPattern = Pattern.compile("^([^\|]*\|)+$");
This regex should work. But if you just want to check if your line ends with a pipe this regex is more simple:
这个正则表达式应该可以工作。但是如果你只想检查你的行是否以管道结尾,这个正则表达式更简单:
Pattern dataPattern = Pattern.compile("^.*\|$");
回答by Scott Rippey
It looks like you're using ^
at the end of the line, but you should be using $
instead.
看起来您是^
在行尾使用,但您应该$
改为使用。
"(.+)\\|$"
"(.+)\\|$"