I have a parser that applies regex rules on a given string. The first match of each rule is removed and placed into a node.
One of the rules tries to find variables, which are in between |
symbols consisting of only digits, letters, underscore and dash. The rule that does this is the following: (?:\|)[a-zA-Z0-9_-]+(?:\|)+?
For example we have the expression: part1+250+|part2|+part3
It matches on |part2|
which is a variable.
Now I need another rule that matches constants, which can consist of only (digits, letters, underscore, dash and spaces). But it should not match on the variables.
Taking a look at the same example expression, it should match the following: part1
,250
,part3
It should match anything that is (digit, letter, space, underscore, dash) and is not between |
symbols.
How can I achieve this using a single regex expression?
Here is a single-pattern implementation of your solution:
String input = "part1+250+|part2|+part3";
Pattern p =
// | group 1 ("variable")
// | | positive look behind for "|"
// | | | character class for allowed characters
// | | | | quantifier for 1+ instance, greedy
// | | | | | positive lookahead for "|"
// | | | | | | OR
// | | | | | || group 2 ("constant")
// | | | | | ||| negative lookbehind for "|"
// | | | | | ||| | allowed chars
// | | | | | ||| | | quantifier
// | | | | | ||| | | | negative
// | | | | | ||| | | | lookahead
// | | | | | ||| | | |
Pattern.compile("((?<=\\|)[ a-zA-Z0-9_-]+(?=\\|))|((?<!\\|)[ a-zA-Z0-9_-]+(?!\\|))");
Matcher m = p.matcher(input);
while (m.find()) {
System.out.printf("Variable: %s%nConstant: %s%n", m.group(1), m.group(2));
}
Output
Variable: null
Constant: part1
Variable: null
Constant: 250
Variable: part2
Constant: null
Variable: null
Constant: part3
Notes
null
s and infer whether a "variable", a "constant", or nothing's been found.이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다