正则表达式匹配管道描述的文件

Jas*_*son 3 java regex

我需要一个正则表达式的帮助来检查一行是否匹配一行管道描绘的数据.数据将以管道结束,并且不会引用.有些字段是空的.

这是我正在尝试使用的内容:

Pattern dataPattern = Pattern.compile("(.+)\\|^");
Run Code Online (Sandbox Code Playgroud)

以下是一个示例数据行:

GJ 3486|||121.10766667|-83.23302778|295.84892861999998|-24.832649669999999||-0.48399999999999999||.371|2MASS J08042586-8313589|8.9700000000000006|8.3539999999999992|8.1110000000000007||2MASS||
Run Code Online (Sandbox Code Playgroud)

因为我只想看看这条线是否与图案相匹配,我认为我想出的那条线会找"哇哇哇哇哇哇哇哇哇".显然不是......任何人都可以帮助我吗?

贾森

Fai*_*Dev 7

^(.*?\|)*$
Run Code Online (Sandbox Code Playgroud)

试试这个.

"
^        # 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)
"
Run Code Online (Sandbox Code Playgroud)

正则表达式的一些问题:

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