2

I need to split a String using these possible tokens (<=,<,>=,>,==), by using a regex. Here are two examples of what I'm looking to achieve:

1.
Input: 123<=456
Result :[ 123, <=, 456]

2.
Input: 123<456
Result: [123, <, 456]

I wrote the following regex that works for the first example, but doesn't work for the second one. What's wrong with it?

Regex: ((?<=((<=)|(==)|(>=))|(?=((<=)|(==)|(>=))

2
  • Can you post some code to show how you achieve the first result? Commented Oct 17, 2018 at 17:16
  • import java.util.Arrays; public class Simple{ public static void main(String args[]){ String s1 = "abc<=def"; System.out.println(Arrays.toString(s1.split("((?<=((<=)|(==)|(>=)))|(?=((<=)|(==)|(>=))))")));}} Commented Oct 17, 2018 at 17:29

2 Answers 2

3

Result = [Group 1, Group 2, Group 7] using

(\d+)((([><])(=?))|(==))(\d+)

As shown here https://regex101.com/r/kIDOBy/1

enter image description here

Results do not match for these:

123>>232
123<<232
123=232
Sign up to request clarification or add additional context in comments.

4 Comments

This matches 123|323 which is incorrect. You need to take away | from inside square brackets. Only one character matches among all characters in a square bracket and you don't need Pipe '|' character inside square bracket
You could have changed it to simply this (\d+)((([>=<])(=?)))(\d+) simpler than what you did (\d+)((([><])(=?))|(==))(\d+). You really didn't need an OR in your regex.
That way even 123=232 would match, which isn't expected behavior.
Yes, correct. BTW OP just needed a simple regex because these operators are applied to numbers and like OP said he just wanted it for (<=,<,>=,>,==) operators, which is why I came up with a simple regex initially but someone's comment lead to this over effort :-| which isn't even needed by OP
1

You can use this regex and capture, group 1 2 and 3.

(\d+)([<=>]{1,2})(\d+)

Check here,

https://regex101.com/r/hhc1t9/1

Explanation:

  1. Group1 (\d+) captures any digits before the expression.
  2. Group2 ([<=>]{1,2}) captures the expression which can be either of this as you wanted (<=,<,>=,>,==)
  3. Group3 (\d+) again captures any digits after the expression.

Edit:

Ok, someone mentioned it should not capture additional possible expressions though that doesn't seem to be the real case because OP hasn't said so secondly, those would be invalid operators. You can't have >> or << as a valid operator. But nevermind, One can use this updated regex to only match the expressions ONLY given by OP. This just looks a bit complex and looses readability and doesn't seem it is really needed by OP but here it is.

(\d+)((?:(?:<=)|<|(?:>=)|>|(?:==)))(\d+)

Play with updated regex here

https://regex101.com/r/hhc1t9/3

2 Comments

This regex would also capture additional tokens such as =, =>, >> which are not specified in the OP's question.
Well, OP has clearly given his possible tokens. If you want it to work only and only for OP mentioned cases, then it is easy to modify the regex. and use this in second group (<=|<|>=|>|==)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.