2

I was wondering if the concept of regex was applicable only to String types in Java, or can be applied to numerical values too. For instance:

  • Find all integers from a list of integers which are greater than x but less than y

  • Find all integers from a list of integers greater than x

  • Find all integers from a list of integers which are less than ( x-10)

Does the concept exist for numerical values too but isn't implemented in Java, or is it too challenging to implement for numerical values?

As far as I understand, regex should be applicable to any place where searching is involved (irrespective of value types).


As suggested in answers and comments, Java 8's Stream functionally does what I am looking for (via filters etc), but my question was mainly for missing short hand forms in the list of regex patterns for numerical values; such as, if > x exists as a pattern, that would have been easier.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • 1
    For all three cases, you should use comparison operators. To use regex on numerical values, you need to first cast/coerce them to **String**. In other words, **regex can only be used on Strings** – Tushar Jan 04 '16 at 03:23
  • 2
    Sounds like you want to use a [`Stream`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html) – Elliott Frisch Jan 04 '16 at 03:24
  • @ElliottFrisch: Yes , I read briefly about `Stream` and that seems what I am looking for. – Sabir Khan Jan 04 '16 at 03:28
  • most of the time, down voters don't explain but I again ask, why? I guess, my question was not clear enough, probably, it is after EDIT section. This is not to question your judgement but to correct myself. – Sabir Khan Jan 04 '16 at 03:45

2 Answers2

4

A regular expression is really in the same family as a finite-state machine, with the idea being that you have an alphabet of all known characters, and the ability to represent capture states through the expression itself.

For example, there are many things that the expression Colorado License Plate Number [A-Z]{3}-\d{3} could capture, but it requires the string 'Colorado License Plate Number ' to be part of that state. And yes, the space is included.

The expressions you're representing don't have any state-specific information to them; namely, they're checking exclusively for a boolean response.

  • Find all integers from a list of integers which are greater than x but less than y

    • This indicates a filter operation through a list which, given an element of the list i, finds x < i < y
  • Find all integers from a list of integers greater than x

    • This indicates a filter operation through a list which, given an element of the list i, finds i > x

... and so forth.

Those are better represented as predicates. Google Guava has an entire class dedicated to them, and lambdas in Java 8 can substitute for them in streams.

So, yes, regular expressions really only apply to strings. If you want a more complicated FSM, then it's possible to write one, but not using your local regex engine.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Thanks a lot for distinguishing between state and filter. Idea behind asking a question is to learn something new and I guess, I did. Thanks. – Sabir Khan Jan 04 '16 at 03:50
1

A regular expression is a concept that applies specifically to strings (this is not specific to Java).

It sounds like what you want is a predicate, which is a function that takes a value and returns true or false and thus can be used to filter a collection of objects. Java 8 streams support such filtering, and Groovy can apply filters to Java collections (actually anything iterable).

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152