简体   繁体   English

通过NOT开头的String匹配正则表达式?

[英]Match Regex by NOT beginning of String?

I have a string of a score between two people. 我有一串两个人之间的分数。 The database I'm pulling these scores from puts a dash between the scores... So normally the score would look something like: 我正在拉动这些分数的数据库在分数之间划线......所以通常分数看起来像这样:

2-3

I am trying to split these two scores into a String[] in Java. 我试图将这两个分数分成Java中的String[]

match.get("scores").toString().split("-", 1)

The problem is, what happens if the scores are negative: 问题是,如果得分为负,会发生什么:

-2--3

What I would need to do now is split by the first dash it finds that ISN'T the first character in the string. 我现在需要做的是通过它发现的第一个破折号分裂,它不是字符串中的第一个字符。

How would I change my .split(RegEx, 1) so that I can match the dash as not the first character? 我如何更改我的.split(RegEx, 1)以便我可以将短划线与第一个字符匹配?

You can split using this: 您可以使用此分割:

.split("(?<=\\d)-");

(?<=) is a positive look-behind. (?<=)是一个积极的后顾之忧。 Just google positive-lookbehind regex and you should find plenty of information about it. 只是google positive-lookbehind regex ,你应该找到很多关于它的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM