Regex to remove string between two characters (exclusive) - java

If I have a string blah.t!#Z8-st? how can I remove the string between the characters . and ? in Java?
so the resulting text would be blah.?
I have got the following but it is inclusive:
String s = "blah.t!#Z8-st?";
System.out.println(s.replaceAll("\\..*?\\?", ""));

Since this looks like a contest to "write the most complex regex", let me submit my modest contribution.
System.out.println("blah.t!#Z8-st?".replaceAll("\\..*?\\?", ".?"));
Update
After your edit it appears that you already have the exact same regex as mine, so the only thing you need to fix is the "replace with" part, where you keep the delimiters.

You can use this:
String repl = string.replaceAll("^(.*?\\.)[^?]*", "$1");

Use String replaceAll method with following pattern:
Pattern
(?<=\.).*(?=\?)
Code
String s = "blah.t!#Z8-st?asdas";
System.out.println(s.ReplaceAll("(?<=\\.).*(?=\\?)", ""));

Related

Split a string using a string as delimiter

Is it possible to split a string using a string as delimiter? If yes, how?
Example :
String myString = "hello_world<;>goodbye<;>foo";
myString.split("<;>");
//returns "hello_world", "goodbye" and "foo"
The example in your question works exactly, however this may be coincidental. Keep in mind that the String.split(...) method accepts a RegEx parameter, not a String delimiter.
Check out the RegEx documentation here: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum
yes, exactly as you have in your code.

how to remove a + (plus) mark from a String using a regex in java

I Need to remove the plus (+) from a String using regex in java.
+876755444 should be able to remove + then it should display like 876755444.
I was trying to do this and new to regex as well.
Can anybody tell me how to do this.
The answer of #nafas should be correct:
myString.replaceAll("\\+","");
This would remove all non-numeric chars.
string.replaceAll("\\D", "");
String s = "+876755444";
System.out.println(s.replace("+", ""));

Using Regexp in Java to remove some text

It is maybe a simple question. But I tried a lot of Regexp combinations and still not worinkg. My problem is: I have words like: Test=move or Testing=move
I would like to remove the text 'Test=' or 'Testing='. In other words i need only the 'move' text after the '='. What is the best way to do that in Java? Thanks.
I think that for this problem, the split(string regex) is better suited:
String str = "Test=move";
System.out.println(str.split("=")[1]);
I would replace \w+= with "" - this will get rid of any work preceding an equals sign.
myString.replaceAll("\w+=", "");
If the string before the equals sign has more than just letters you can add them to an optional selection:
myString.replaceAll("[\w-\.\d]+=", "");
This will remove any word with letters, numbers, hyphens and periods.

Replacing occurrences of generic string

I never was good enough with regex, and I assume this is the job for it.
I have a link like www.somelink/phoyto.jpg/?sz=50
I need to replace 50 with my value, let's say 100. Trouble is, that I cannot be sure, that this will be always sz=50 and not sz=150 or sz=10 or any other value.
What I need is to find an occurence of string contains of 'sz' + number and replace it with 'sz=100'.
Sure, I can do that 'manually" in some for loop, but that wouldn't be nor smar nor efiicient.
str = "www.somelink/phoyto.jpg/?sz=50";
str.replaceall("sz=\\d+", "sz=100");
\d is the java pattern for digit. + stands for one or more digits. replaceall replaces all occurrences of sz=<number>.
Here is a handy online regex tester for java: http://www.regexplanet.com/advanced/java/index.html
This should work:
String link = "www.somelink/phoyto.jpg/?sz=50";
link = link.replaceFirst("sz=\\d+", "sz=100");
System.out.println(link);
It's pretty simple, and this pattern should work:
(sz=\d+)
Code:
String result = searchText.replaceAll("(sz=\\d+)", "sz100");
Example:
http://regex101.com/r/mB3xT9

how to remove only charactres in a given string in java?

I am trying to remove only [A-z|a-z] like this:
String input ="A021001208A 711100609C 01111";
String clean = input.replaceAll("\\D+^\\s+","");
System.out.println(clean.toString());
but the above code also removes the spaces; I don't want to remove space.
The expected output is:
021001208 711100609 01111
Please help me to formate the reg-ex to remove only characters.
Just replace [a-zA-Z] then:
String clean = input.replaceAll("(?i)[A-Z]+","");
(?i) is ignore case embedded flag expression.
Rather than use a positive character class, use a negated one. The regex you want is:
[^\d\s]
Which means "any character other than a digit or a whitespace".
When coded as java, it looks like:
String clean = input.replaceAll("[^\\d\\s]","");
Try this it will replace all occurence of alphabet from the given string.
String clean = input.replaceAll("[^a-zA-Z]", "");
You have to use [a-zA-Z] regular expression. So your .replaceAll() method will look like as below :
String clean = input.replaceAll("[a-zA-Z]","");

Categories