我正在尝试使用preg_split,如下所示:
patternList.setCellFactory(l -> new TextFieldListCell<Pattern>(new StringConverter<Pattern>() {
@Override
public String toString(Pattern pattern) {
return pattern.toString();
}
@Override
public Pattern fromString(String string) {
try {
return Pattern.compile(string);
} catch (PatternSyntaxException e) {
return null;
}
}
}) {
@Override
public void commitEdit(Pattern pattern) {
if (!isEditing()) return;
PseudoClass errorClass = PseudoClass.getPseudoClass("error");
pseudoClassStateChanged(errorClass, pattern == null);
if (pattern != null) {
super.commitEdit(pattern);
}
}
});
patternList.setOnEditCommit(e -> patternList.getItems().set(e.getIndex(), e.getNewValue()));
我无法捕获数组[0]值。以下是输出:
$tarea = "13.3R4.2";
$keywords = preg_split("/[0-9]*.[0-9][a-zA-Z]/", $tarea);
print_r ($keywords);
我想要捕获数组的索引。我不确定我在这做什么错。请帮助!!
我期望的输出是:
Array ( [0] => [1] => 4.2 )
由于
答案 0 :(得分:1)
我认为你误解了preg_split()
的目的。它在指定的分隔符上拆分字符串,这意味着它不是为了返回分隔符而设计的。您希望使用preg_match()
返回匹配的子字符串:
$tarea = "13.3R4.2";
preg_match_all("/[0-9]+\.[0-9][a-z]?/i", $tarea, $matches);
print_r($matches);
答案 1 :(得分:1)
下面的代码解决了这个问题:
$tarea = "13.3R4.2";
$keywords = preg_split('/(\d*\.\d\w?)/',$tarea, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r ($keywords);
输出:
Array ( [0] => 13.3R [1] => 4.2 )
谢谢大家的帮助!!!