Java Regex Question

In Java - I need to search / check the input string for a number or for a specific string. it must be one of them. For example, if I have these input lines:

cccccccc 123 vvvvvvvvv jhdakfksah
cccccccc ABC vvvvvvnhj  yroijpotpo
cccdcdcd 234 vcbvbvbvbv lkjd dfdggf
ccccbvff ABC jflkjlkjlj fgdgfg

I need to find 123, ABC, 234, ABC

to find the number that I could use regex: "\ d +", but look for any of them. How to combine them?

+3
source share
6 answers

You can specify alternatives in the regular expression with the symbol |:

\d+|[ABC]+

, -, "" ( ), , . , , :

" (\d+|[ABC]+)"

( , ). lookbehind:

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

, .

+2

, , , - , . String.indexOf :

String line = ...;
int start = line.indexOf(" ") + 1;
int end = line.indexOf(" ", end);
String word2 = line.substring(start, end);

, ,

String word2 = line.split(" ")[1];
+2

- ...

^[^ ]* ([^ ]*) .*$
+2

Something like this: [0-9] + | [a-zA-Z] + or possibly [0-9] + | (ABC) +; You don’t know what your rule for symbols is.

0
source

Try this regex:

"(\d+|ABC)"
0
source

Try this regex:

^\w+\W+(\w+)

And make your expression according to the beginning of line c ^.

0
source

Source: https://habr.com/ru/post/1724872/


All Articles