gpt4 book ai didi

java - 匹配竖线分隔文件的正则表达式

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:15:38 31 4
gpt4 key购买 nike

我需要正则表达式方面的帮助来检查一行是否与一行管道描述的数据相匹配。数据将以管道结尾,并且不被引用。一些字段将为空。

这是我正在尝试使用的:

Pattern dataPattern = Pattern.compile("(.+)\\|^");

这是一行数据示例:

GJ 3486|||121.10766667|-83.23302778|295.84892861999998|-24.832649669999999||-0.48399999999999999||.371|2MASS J08042586-8313589|8.9700000000000006|8.3539999999999992|8.1110000000000007||2MASS||

因为我只想看看该行是否与模式匹配,所以我认为我想出的那个会寻找“blah blah blah |”。显然不是...有人可以帮我吗?

杰森

最佳答案

^(.*?\|)*$

试试这个。

"
^ # 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)
"

你的正则表达式的一些问题:

  • 拳头它不是重复的,你应该重复这个模式,因为你有很多列。
  • 你匹配了一些东西,然后你匹配了字符串的开头。不可能,这永远不会匹配。
  • 您总是希望匹配一个字符,但您说可能会有空列。而是使用 * 量词。

关于java - 匹配竖线分隔文件的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7959929/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com