Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My objective is to separate the numbers from the string, but in my array's first position i get a blank space. So i need help for that not to happen.
str1 = "Y=9x1+29x2";
String[] split2 = str2.split("[^-?.?0-9]+");
Blank space at the start is due to presence of non-digit character at the start of your input.
You can remove all non-digits at start before splitting:
String linha = "Y=9x1+29x2";
String[] split = linha.replaceFirst("[^-.\\d]+", "").split("[^-.\\d]+");
for (String tok: split2)
System.out.println(tok);
Output:
9
1
29
2
I think your question is rather vague, but after looking at it, I'm guessing that you want to extract the numbers out of the string, where a "number" has this format: an optional minus sign, followed by an optional decimal point, followed by one or more digits. I suspect you also want to include numbers that have digits followed by a decimal point followed by more digits.
I'm guessing this is what you want, because of the ? you put in your regex. The problem is that inside square brackets, ? doesn't mean "optional", and it doesn't mean "zero or one of something". It means a question mark. The regex [^-?.?0-9] means "match one character that is not a digit, a period, a hyphen, or a question mark". A pattern in square brackets always matches one character, and you tell it what characters are OK (or, if you begin with ^, what characters are not OK). This kind of "character set" pattern never matches a sequence of characters. It just looks at one character at a time. If you put + after the pattern, it still looks at one character at a time; it just does so repeatedly.
I think what you're trying to do is to take a pattern that represents a number, and then say "look for something that doesn't look like that pattern", and you tried to do it by using [^...]. That simply will not work.
In fact, split() is the wrong tool for this job. The purpose of split is to break up a string whose delimiters match a given pattern. Using it when the strings you want to keep in the array match a given pattern doesn't work very well, unless the pattern is extremely simple. I recommend that you create a Matcher and use the find() method in a loop. find() is set up so that it can find all matching substrings of a string if you call it repeatedly. This is what you want to accomplish, so it's the right tool.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using java and have string which could have multiple spaces and equal to "=" sign as shown below.
String temp = "[name='FPC:CPU']/XM chip/allocate";
This temp string will passed to some other program which is failing because of space and equal sign.
How can i escape space and "=" character?
My desired out put from original string
[name='FPC:CPU']/XM chip/allocate
to
[name\='FPC:CPU']/XM\ chip/allocate
Wondering how can i do that using temp.replaceAll
That should be pretty straight forward.
System.out.println("foo bar=baz".replaceAll("([ =])" "\\\\$1"));
Should print this
foo\ bar\=baz
The parenthesis in the regular expression form a capturing group, and the character class [ =] will capture spaces and equal signs.
In the replace expression, the $1 refers to the first capturing group. The only thing that gets a bit tricky is escaping the backslash.
Normally, in a regular expression replacement the backslash itself is an escape character. So you'd need two of them together to insert a backslash, however backslash is also an escape in a Java String, so to put two backslashes into a Java String (to form the regular expression escape), you must insert four backslashes. So that's how you end up with "\\$1".
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
In a Java app, I use this regex: (\w+)_\d to match patterns of this form:
apples_1
oranges_2
and then I use the first capturing group value (apples, oranges).
However, I now have a new request to also match these strings:
applesdrp_1
orangesdrp_2
where 'drp' is a fixed 3 character string, and the same values as before need to be captured: apples, oranges
So for example, if I use this regex: (\w+)(?:drp)?_\d
it will do the work on apples_1, but not for applesdrp_1.
Is there a way to do that with a regex?
You can use a non-greedy quantifier:
(\w+?)(?:drp)?_\d
In this way \w+? will take characters until it find "drp_N" or "_N" (where N is a digit).
If you use a greedy quantifier, \w+ takes all possible character (including the underscore and the digit since they are included in \w) and then gives back characters one by one until (?:drp)?_\d succeeds. But since (?:drp)? is optional, the regex engine stops to backrack when it find _N.
Yes, you can - one way would be using a negative lookbehind, to make sure, that the drp is forced outside the group, if it is present
(\w+)(?<!drp)(?:drp)?_\d+
See https://regex101.com/r/jJ1rM4/3 for a demo
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
As the title states, i want to know how i can check if a string consists of 2 integers with a blank space in between them in Java.
As an example:
0 2, should return true.
0 abc, should return false.
abcsd, should return false.
And so on...
If it is to any help, I am getting my string from a text file with a buffered reader. Maybe there is a more direct and easier way?
Thank you in advance
You could use string.matches method.
string.matches("\\d+\\s\\d+");
DEMO
\d+ matches one or more digits. So this asserts that the input string must contain a number at the start.
\s matches a space character. So this asserts that the input string must contain a space at the middle.
\d+ matches one or more digits. So this asserts that the input string must contain a number at the end.
Since matches method tries to match the whole input string, you don't need to add start and end anchors to your regex.
Because you haven't posted your own code here, I assume that you haven't made much research into it, have you? First of all, will you use only 1-digit numbers? Here's how you should start: http://docs.oracle.com/javase/tutorial/java/data/converting.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have some code:
firstWord = sentance.substring (0, sentance.indexOf(' '));
secondWord = sentance.substring(sentance.indexOf(' ') + 1);
the code is used for selecting the first word out of a string without the use of arrays.
However I am wondering if I can further fool-proof my code by implementing a safeguard so that if the user inputs two spaces, the code will take the group of white space and count it as one unit.
Is this possible without the use of arrays, or loops?
For example the user would input this:
"Hello 2spaces there"
the user accidentally inputted two spaces in the beginning which will mess the program up when it tries to take the second word i think.
remove multiple space with single space as :
String str="Hello world this is string";
str=str.replaceAll("\\s+", " ");
.......// do whatever you want
Your code will not take the first word out of string only if the first character of the string is a space, or before the first space is not a word, or there is no space, for example " hello" -> "", "!##! blah" -> "!##!", "asdasd" -> ""
y.indexOf(x) returns the index of the first occurrence of x in y.
Your solution is mostly foolproof, but it will fail to get the first word if there are spaces before it, or there is no whitespace in the specified string, because indexOf would return -1.
You should call the .trim() method on the string object you want to get the first word of, it will remove the whitespace around the string, and then add a single space character at the end of the string.
str = "Hello I'm your String";
String[] splited = str.split("\\s+");
You can use arrays they are not that bad.
If you really must avoid using an array, you could use sentance.replaceAll("\\s+", " "); first to collapse all sequences of consecutive whitespace into singleton spaces.
(Similarly, you would want to trim() leading and trailing whitespace as well.)
If you just want to remove trailing and leading whitespace use .trim()
str = str.trim()
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Regular expression in java:
'String'.replaceAll("([aeioucgjkqsxyzbfpvwdtmn1234567890])\\1+", "$1")
Can someone explain what the different characters do?
Explanation:
[aeioucgjkqsxyzbfpvwdtmn1234567890] Matches a single character in the list.
([aeioucgjkqsxyzbfpvwdtmn1234567890]) Capturing group around the char class would capture that single character.
\1+ \1 is a pointer to refer the chars inside the group index 1. In our case, a single character is captured so it refers to that single character. \1+ means one or more occurrences of the characters inside group index 1.
For Example:
aaaa
The above regex would capture the first character and check if the following one or more characters are same as the first character which was captured. If yes, then the whole duplicated chars are replaced by a single char(which was inside group index 1 ), that is aaaa was replaced by a single a
DEMO
All letters that are listed between brackets will be replaced by $1 if after them comes a \1, which is a literal backslash one. The plus sign (+) means 1 or more.
Any sequence of 1 or more of the characters inside the brackets [...] will be replaced with $1.
For instance, this will remove all those characters from your string:
System.out.println(Str.replaceAll("([aeioucgjkqsxyzbfpvwdtmn1234567890])\1+", ""));