Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this String
English>Arts>Photography
Want regular expression to delete all after last (>) ,Want output to be like this
English>Arts>
Many Thanks,
use String.lastIndexOf() instead of String.indexOf(). no need to go to regexps for this
Use LastIndexOf as mentioned below :
Substring(0, [YourString].LastIndexOf('>'));
You can use substring and lastIndexOf as shown below:
String s = "English>Arts>Photography";
System.out.println( s.substring(0,s.lastIndexOf('>') ));
If you really didn't want to use .lastIndexOf() (i don't know why you wouldn't). The regex would have been:
String input = "English>Arts>Photography";
Pattern p = Pattern.compile("(.*>)[A-Za-z]+");
Matcher m = p.mathcer(input);
if (m.matches()) {
String output = m.group(1);
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a String in java Which contains following characters ..
'2010-12-04' and '2013-12-03' and wwid='1234'
Now as per my need i have to remove the starting characters of this and make it like..
and wwid='1234'
I tried it through substring concept where i tried to give the starting point where it needs to be deleted and after that add it into String but i am not able to get it..
Please help me to solve this.
Thanks in advance
You can first find the last Indexof String and, and make it as start position to do substring method.
Try
String text = "'2010-12-04' and '2013-12-03' and wwid='1234'";
text = text.substring(text.lastIndexOf("and"));
System.out.println(text);
Output in console:
and wwid='1234'
Combine IndexOf() and Sustring() or use replace(UndesiredCharSequence, "")
Actually substring should work. One thing you need to consider is that substring does not change the actual string but returns a new string. So use:
String newString = str.substring(30);
You could try this:
String input = "'2010-12-04' and '2013-12-03' and wwid='1234'"
String myNewString = input.substring(numberOfPositionsToRemove);
In your case it would be:
String input = "'2010-12-04' and '2013-12-03' and wwid='1234'"
String myNewString = input.substring(30);
Or to make it more dynamic you could use:
String input = "'2010-12-04' and '2013-12-03' and wwid='1234'"
int index = input.lastIndexOf("and");
String myNewString = input.substring(index);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having an activity that return string message
String value of my file id like
file!^#123456
I am interesting only the number of that file id i see some regex expression but i did not understand properly.
if I get it right regex for "I am interesting only the number of that file id" will be : [0-9]+ or with Java Predefined Character Classes; [\d]+.
Try this.
String str = "file!^#123456";
System.out.println(str.replaceAll("\\D+",""));
You could go at it using substring on it's own:
String s = "file!^#123456";
System.out.println(s.substring(s.indexOf("#") + 1, s.length()));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm looking for a java regex pattern that exclude any text that have two or more consecutive dashes or two or more consecutive spaces.
Assuming that you will use your regex in mechanism similar to matches method you may be looking for something like
^(?!.*(--| )).*$
try this
s = s.replaceAll("( )+|(-)+", "$1$2");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Regex [abcd] can take any one character out of a, b, c and d.
What if I want to take any one string out of abc, def and ijk.
So something like ["abcd" "def" "efg"] (but this obviously doesn't work).
How would I do this in Java?
your regex could look like this:
(abcd|def|efg)
[] is only for single characters.
You can use | for multiple characters (X|Y means "Either X or Y"):
abcd|def|efg
In case there's anything else in the regex, you'd want to surround it with brackets:
other(abcd|def|efg)stuff
The above matches these strings:
otherabcdstuff
otherdefstuff
otherefgstuff
Where-as:
otherabcd|def|efgstuff
would obviously match these strings:
otherabcd
def
efgstuff
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to remove all alphabetical characters from a string usign a regular expression in java/android?
val = val.replaceAll("/A/z","");
Try this:
replaceAll("[a-z]", "");
Also have a look here:
Replace all characters not in range (Java String)
This will remove all alphabetical characters
String text = "gdgddfgdfh123.0114cc";
String numOnly = text.replaceAll("\\p{Alpha}","");
Have a look into Unicode properites:
\p{L} any kind of letter from any language
So your regex would look like this
val = val.replaceAll("\\p{L}+","");
To remove also combined letters use a character class and add \p{M}
\p{M} a character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.)
Then you end here:
val = val.replaceAll("[\\p{L}\\p{M}]+","");