3

In Java - I need to search/validate an input string for a number or for a specific string. it must be one of them. For example - If I have this 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 look for the number I could use regex: "\d+" but to look for either of them How can I combine them?

1
  • 2
    Are the examples "real", or made up? Looks to me like you don't need to use regex at all, but a simple substring operation would be enough... Commented Dec 6, 2009 at 17:27

6 Answers 6

2

You can specify alternatives in a regular expression by using the | character:

\d+|[ABC]+

In your specific example, the string you want is seemingly always the second "word" (delimited by a space), so it can be beneficial to include the space in the regular expression to look for it. Either by using a capturing group to capture the part you actually want to extract:

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

(I used quotation marks to delimit the regex). Or by using a lookbehind assertion:

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

which will only match the desired portion, but still requires the space before it.

Sign up to request clarification or add additional context in comments.

Comments

2

As at least one other answer has mentioned, it looks like you're picking out the second word in a space-delimited string, and regular expressions are more work than is necessary for that task. String.indexOf would be enough:

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

or, now that I think about it

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

Comments

2

You could have something like this...

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

Comments

0

Something like this: [0-9]+|[a-zA-Z]+ or maybe [0-9]+|(ABC)+; not sure what your rule for the characters is.

Comments

0

Try this regular expression:

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

And make your expression to match the begin of a line with ^.

Comments

0

Try this regular expression:

"(\d+|ABC)"

Comments

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.