Check if a string has a word followed by a number - java

I have strings like:
String test="top 10 products";
String test2="show top 10 products";
Is there a way to check if the word "top" has a number following it? If so, how to get that number to another string?
I'm thinking about using indexOf("top") and add 4 to that and try to get the next word. Not sure how it will work. Any suggestions?

If you only want to extract a possible number after single / first occurrence of "top", that's a viable way. Don't forget to check for existence of the word, and that there's something behind it at all.
You can also use regular expression for this, which will need a bit less error checking:
top\\s+([0-9]+)
You could even make a Pattern out of this, and then iterate the Matcher.find() method and extract the numbers for multiple matches:
Pattern pat = Pattern.compile("top\\s+([0-9]+)");
Matcher matcher = pat.matcher("top 10 products or top 20 products");
while (matcher.find()) {
System.out.println(matcher.group(1));
}

An evil regex can help you.
String test="top 10 products";
System.out.println(test.replaceAll(".*?\\w+\\s+(\\d+).*", "$1"));
O/P :
10
Note : This will return the entire String in case there is no "Word[space]digits" in the String. You will have to do a length check for the actual String and the returned String. If the length is same, then your String doesn't contain the expected pattern.

Related

Java Regex Look-Behind Doesn't Work

So I am working on regex comparing phone numbers and this is the result:
(?:(?:0{2}|\+)?([1-9][0-9]))? ?([1-9][0-9])? ?([1-9][0-9]{5})
As you can see there are spaces between the numbers. I want them to appear only when there is some other number before the space so:
"0022 45 432345" - should match
"45 345678" or "560032" - should match
" 324400" - shouldn't match because of the space in the beginning
I've been reading different tutorials about regexes and found out about look-behinds, but simple construction like that(just for test):
Pattern p2 = Pattern.compile("(?<=abc)aa");
Matcher m2 = p2.matcher("abcaa");
doesn't work.
Can you tell me what's wrong?
Another problem is - I want a character only happen when it is THE FIRST character in a string, otherwise it shouldn't occur. So the code:
0043 022 234567 should not work, but 022 123450 should match.
I'm stuck right now and would appreciate any help a lot.
This should work just fine. The spaces are moved into the optional groups and are themselves optional. This way, they only match if the group before them is present, but even then they are still optional. No look-behind required.
(?:(?:(?:00|\+)?([1-9][0-9]) ?)?([1-9][0-9]) ?)?([1-9][0-9]{5})
Lookbehind is a zero length match.
The javadoc for the Matcher.matches method determines if the whole String is a match.
What you're looking for is something the Matcher.find and Matcher.group methods. Something like:
final Pattern pattern = Pattern.compile("(?<=abc)aa");
final Matcher matcher = pattern.matcher("abaca");
final String subMatch;
if (matcher.find()) {
subMatch = matcher.group();
} else {
subMatch = "";
}
System.out.println(subMatch);
Example.

How to create a Regex to find exact String length?

Having these cases:
12345678901234
123456789012345
1234567890123456
12345678901234567
I need to find the String which has exact 15 chars length.
Until now I made this code:
String pattern = "(([0-9]){15})";
Mathcer m = new Mathcer(pattern);
if (m.find()){
System.out.println(m.group(1));
}
The results were like this:
12345678901234 (not found which is GOOD)
123456789012345 (found which is GOOD)
1234567890123456 (found which is NOT GOOD)
12345678901234567 (found which is NOT GOOD)
How can I create a regex which can give me result of exact 15 like I thought this regex can give me. More then 15 is not acceptable.
Mark the start and the end of the string using the ^ and $ anchors:
String pattern = "^([0-9]{15})$";
^ matches the position at the beginning of the string
$ matches the position at the end of the string
Without these anchors, you're only looking for 15 consecutive digits anywhere within the string. Matching strings can additionally have more digits (or even contain letters), though, and still match.
(Also, your inner pair of parentheses is superfluous — I've removed it. If you're accessing the value of the entire match rather than the value captured by the first group, you can even emit the other parentheses: "^[0-9]{15}$")
Regex101 Demo
Just add a start and end to your regex:
^(([0-9]){15})$
The ^ means "beginning of string"
The $ means "end of string"
Therefore, there can only be 15 numbers in the string.
For more regex operators in Java, see the Pattern documentation
Simply use matches() instead of 'find()'

String split method returning first element as empty using regex

I'm trying to get the digits from the expression [1..1], using Java's split method. I'm using the regex expression ^\\[|\\.{2}|\\]$ inside split. But the split method returning me String array with first value as empty, and then "1" inside index 1 and 2 respectively. Could anyone please tell me what's wrong I'm doing in this regex expression, so that I only get the digits in the returned String array from split method?
You should use matching. Change your expression to:
`^\[(.*?)\.\.(.*)\]$`
And get your results from the two captured groups.
As for why split acts this way, it's simple: you asked it to split on the [ character, but there's still an "empty string" between the start of the string and the first [ character.
Your regex is matching [ and .. and ]. Thus it will split at this occurrences.
You should not use a split but match each number in your string using regex.
You've set it up such that [, ] and .. are delimiters. Split will return an empty first index because the first character in your string [1..1] is a delimiter. I would strip delimiters from the front and end of your string, as suggested here.
So, something like
input.replaceFirst("^[", "").split("^\\[|\\.{2}|\\]$");
Or, use regex and regex groups (such as the other answers in this question) more directly rather than through split.
Why not use a regex to capture the numbers? This will be more effective less error prone. In that case the regex looks like:
^\[(\d+)\.{2}(\d+)\]$
And you can capture them with:
Pattern pat = Pattern.compile("^\\[(\\d+)\\.{2}(\\d+)\\]$");
Matcher matcher = pattern.matcher(text);
if(matcher.find()) { //we've found a match
int range_from = Integer.parseInt(matcher.group(1));
int range_to = Integer.parseInt(matcher.group(2));
}
with range_from and range_to the integers you can no work with.
The advantage is that the pattern will fail on strings that make not much sense like ..3[4, etc.

Substring contatining words up to n characters

I've got a string and I want to get first words that are containing up to N characters together.
For example:
String s = "This is some text form which I want to get some first words";
Let's say that I want to get words up to 30 characters, result should look like this:
This is some text form which
Is there any method for this? I don't want to reinvent the wheel.
EDIT: I know the substring method, but it can break words. I don't want to get something like
This is some text form whi
etc.
You could use regular expressions to achieve this. Something like below should do the job:
String input = "This is some text form which I want to get some first words";
Pattern p = Pattern.compile("(\\b.{25}[^\\s]*)");
Matcher m = p.matcher(input);
if(m.find())
System.out.println(m.group(1));
This yields:
This is some text form which
An explanation of the regular expression is available here. I used 25 since the first 25 characters would result into a broken sub string, so you can replace it with whatever value you want.
Split your string with space ' ' then foreach substring add it to a new string and check whether the length of the new substring exceeds or not exceeds the limit.
you could do it like this without regex
String s = "This is some text form which I want to get some first words";
// Check if last character is a whitespace
int index = s.indexOf(' ', 29-1);
System.out.println(s.substring(0,index));
The output is This is some text form which;
obligatory edit: there is no length check in there, so care for it.

Check if String contains regex match

What i want is to check if there is a number followed by spaces and another number, without any "," in between, anywhere in the String
Currently i am doing this:
Pattern pattern = Pattern.compile("[0-9][\" \"]+[0-9]");
Matcher matcher = pattern.matcher(input);
if(matcher.find()) return false;
and it works just fine. But i was wondering if there is any other simpler way of achieving this?
Since it's an assignment, I won't write out the code, but an alternative solution is:
Split the string on the , token (using String.split())
For each member of the resulting split array:
Trim the leading and trailing spaces from the member
If the trimmed member is an integer (I'll let you figure out how to determine that):
It doesn't meet the criteria you specified
Else:
It's possible that the token could meet your criteria (of containing multiple integers and spaces but no commas. There are several ways you could determine this: do a split on " "; use a while loop, or maybe something else. I'll let you figure that out.
Your solution is good enough, you could try by the positive way like
Pattern pattern = Pattern.compile("^[1-9](,[1-9])*$");
Matcher matcher = pattern.matcher(input);
if(matcher.matches()) return true;

Categories