How to find and replace this string with Eclipse? [closed] - java

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 9 years ago.
Improve this question
I have some line of Java code like this:
Ball b = new Ball(Util.toPx(X*mR), y);
and I would like to replace this line with this
Ball b = new Ball(X*mR, y);
where X is a double number like 1.25, 1.765 etc.
How to obtain this with Eclipse search-replace feature?

I don't know about eclipse, but on sublime text 2 you can obtain it with the search feature with regex enabled. The regex would be something like:
Util\.toPx\((.*?)\)
And if you want to replace this String for something else you could do:
toPx($1)
It would replace the previous string to
toPx(X*mR)
If you have more groups between parentheses, you can use them on the replace expression with $1, $2, etc.

Related

last two occurance of semicolon(;) in given String using Java [closed]

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 1 year ago.
Improve this question
How do I get the after last two semicolon's occurrences of a string ?
Example:
Mobiles;Students;Test;Yes;1234
The output should be
Yes;1234
Using a regex replacement, we can try:
String input = "Mobiles;Students;Test;Yes;1234";
String output = input.replaceAll("^.*;([^;]+;[^;]+)$", "$1");
System.out.println(output); // Yes;1234

Regex pattern to find the occurrence of a pattern [closed]

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 2 years ago.
Improve this question
Edit 1:
Please don't go by the example in a literal way. The key values can be of any string. Need not include "key" and "col" in them.
For example, the string can be the following
Ravi,India,Married;John,Canada,Single;Robert,Spain,Unknown
So, if I pass Ravi as the key, I should get India and Married as the result.
I am trying to use the Java regex pattern to find the details from a given string. The structure of the string is as follows:
Key1,Key1Col1,Key1Col2;Key2,Key2Col1,Key3Col2;Key3,Key3Col1,Key3Col2
where ; is the delimiter for each record.
My requirement is to accept the Key (which is Key1, Key2 etc.) and return the corresponding values like Key1Col1,Key1Col2 etc.
This would do it:
(?<=^Ravi,|;Ravi,)[^;]+
https://regex101.com/r/Tcmzhd/1
The link above shows PHP but it should work in Java no problem.

How to exclude a only substring(not their characters) from the string using java regex? [closed]

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 2 years ago.
Improve this question
When I use [^abc] it excludes a,b,c but I want to exclude only abc not a,b,c.
Just in principle? There are several ways to do it, depending on your context. You could use a negative lookahead, for example, which will assert what the following string must not look like.
Example text:
aba abc abx
Expression:
\b(?!abc)\w{3}\b
Matches:
aba, abx
You should update your question with context if you want specific help.

How to alter Java strings contain only certain alphabetical characters [closed]

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 6 years ago.
Improve this question
how can I alter text string fields in java to only contain certain alphabetical characters (f,-)
in this format: 2f5-4, 2f6, 8f9
Only numbers f numbers and
numbers f number - numbers
You could use a regular expression to check, if the String in your text field is valid:
\d+f\d+(?:-\d+)?
Java code sample
Pattern pattern = Pattern.compile("\\d+f\\d+(?:-\\d+)?");
for (String s : new String[] {
"2f6", "2f9", "6f10", "5f9-2", "3f4-9"
}) {
System.out.println("String: \""+s+"\" match: "+pattern.matcher(s).matches());
}
Regex expression =>
(([0-9]+)([f])([0-9]*)|(([-])([0-9]*)))\w+

Changing the Lengths of Words in a String - Java [closed]

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
I am currently writing a game in Java, where words in a string will have to be all changed to equal 5 characters a word.
eg. I am writing in Java
Iamwr iting inJav a
I wonder if anyone knows how I would do this?
First remove spaces between words.
Then split that resulting String to length with 5.
Add a space after every 5 character.

Categories

Resources