How to remove everything after last hyphen using regex java [duplicate] - java

This question already has answers here:
Remove string after last occurrence of a character
(5 answers)
Closed 3 years ago.
I want to remove everything after last hyphen
branch:resource-fix-c95e12f
I have tried with following command :
replaceAll(".*[^/]*$","");
I want the expected result as branch:resource-fix

Instead of regex, could you use
String::lastIndexOf?
str.substring(0,str.lastIndexOf(‘-‘))

You can use this regex:
-[^-]+$
Example: https://regex101.com/r/dHSNNN/1

You can use the following pattern
-[^-]*$
Demo

This expression might simply work:
-[^-]*$
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "-[^-]*$";
final String string = "branch:resource-fix-c95e12f";
final String subst = "";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println(result);
Another option would be, ^(.*)-.*$ replaced with \\1.
Demo 2
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

Related

Java regex matching in between two pattern [duplicate]

This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 3 years ago.
I've a url like
https://example.com/helloworld/#.id==imhere
or
https://example.com/helloworld/#.id==imnothere?param1=value1
I want to extract the value imhere or imnothere from these URLs.
Pattern.compile("(?<=helloworld\\/#\\.id==).*(?=\\?)");
Problem with this one is it does not found ? (first case) it is not matching the pattern.
Can someone help me to fix this?
Sorry my mistake, I've missed #.id phase in the URL.
This expression should do it:
^.*#==(.*?)(?:\?.*)?$
regex101 demo
It searches for #== and grabs everything after this string, up to a ?, if any. The trick is the lazy *.
The actual match is in group one. Translated to Java, a sample application would look like this:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Sample {
private static final String PATTERN_TEMPLATE = "^.*#==(.*?)(?:\\?.*)?$";
public static void main (final String... args) {
final Pattern pattern = Pattern.compile(PATTERN_TEMPLATE);
final String firstTest = "https://example.com/helloworld/.#==imhere";
final Matcher firstMatcher = pattern.matcher(firstTest);
if (firstMatcher.matches()) {
System.out.println(firstMatcher.group(1));
}
final String secondTest =
"https://example.com/helloworld/.#==imnothere?param1=value1";
final Matcher secondMatcher = pattern.matcher(secondTest);
if (secondMatcher.matches()) {
System.out.println(secondMatcher.group(1));
}
}
}
Ideone demo
If one wants to incorporate the regex to also validate that helloworld/. is present, then one can simply extend the regular expression:
^.*helloworld\/\.#==(.*?)(?:\?.*)?$
regex101 demo
But one should be careful when translating this expression to Java. The backslashes have to be escaped.
I would not use a regular expression for this. It’s a heavyweight solution for a simple problem.
Use the URI class to extract the path (the part between the host and the ?, if any), then look for the last occurrence of ==:
String s = "https://example.com/helloworld/#.id==imnothere?param1=value1";
URI uri = URI.create(s);
String path = uri.getPath();
int idAttributeIndex = path.lastIndexOf("#.id==");
String id = path.substring(idAttributeIndex + 6);
To match after a specific phrase up to the next whitespace, question mark, or end of string, the Java regex is this :
"(?<=helloworld/\\.#==).*?(?![^\\s?])"
https://regex101.com/r/9yraPt/1
If spanning lines, ad (?s) to the beginning.

Using or '|' in regex [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 5 years ago.
I am stuck in a simple issue I want to check if any of the words : he, be, de is present my text.
So I created the pattern (present in the code) using '|' to symbolize OR
and then I matched against my text. But the match is giving me false result (in print statement).
I tried to do the same match in Notepad++ using Regex search and it worked there but gives FALSE( no match) in Java. C
public class Del {
public static void main(String[] args) {
String pattern="he|be|de";
String text= "he is ";
System.out.println(text.matches(pattern));
}
}
Can any one suggest what am I doing wrong.
Thanks
It's because you are trying to match against the entire string instead of the part to find. For example, this code will find that only a part of the string is conforming to the present regex:
Matcher m = Pattern.compile("he|be|de").matcher("he is ");
m.find(); //true
When you want to match an entire string and check if that string contains he|be|de use this regex .*(he|be|de).*
. means any symbol, * is previous symbol may be present zero or more times.
Example:
"he is ".matches(".*(he|be|de).*"); //true
String regExp="he|be|de";
Pattern pattern = Pattern.compile(regExp);
String text = "he is ";
Matcher matcher = pattern.matcher(text);
System.out.println(matcher.find());

java split by bracket and keep the delmiter - RegEx [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 6 years ago.
i am trying to split the string using regex with closing bracket as a delimiter and have to keep the bracket..
i/p String: (GROUP=test1)(GROUP=test2)(GROUP=test3)(GROUP=test4)
needed o/p:
(GROUP=test1)
(GROUP=test2)
(GROUP=test3)
(GROUP=test4)
I am using the java regex - "\([^)]*?\)" and it is throwing me the error..Below is the code I am using and when I try to get the group, its throwing the error..
Pattern splitDelRegex = Pattern.compile("\\([^)]*?\\)");
Matcher regexMatcher = splitDelRegex.matcher("(GROUP=test1)(GROUP=test2) (GROUP=test3)(GROUP=test4)");
List<String> matcherList = new ArrayList<String>();
while(regexMatcher.find()){
String perm = regexMatcher.group(1);
matcherList.add(perm);
}
any help is appreciated..Thanks
You simply forgot to put capturing parentheses around the entire regex. You are not capturing anything at all. Just change the regex to
Pattern splitDelRegex = Pattern.compile("(\\([^)]*?\\))");
^ ^
I tested this in Eclipse and got your desired output.
You could use
str.split(")")
That would return an array of strings which you would know are lacking the closing parentheses and so could add them back in afterwards. Thats seems much easier and less error prone to me.
You could try changing this line :
String perm = regexMatcher.group(1);
To this :
String perm = regexMatcher.group();
So you read the last found group.
I'm not sure why you need to split the string at all. You can capture each of the bracketed groups with a regex.
Try this regex (\\([a-zA-Z0-9=]*\\)). I have a capturing group () that looks for text that starts with a literal \\(, contains [a-zA-Z0-9=] zero or many times * and ends with a literal \\). This is a pretty loose regex, you could tighten up the match if the text inside the brackets will be predictable.
String input = "(GROUP=test1)(GROUP=test2)(GROUP=test3)(GROUP=test4)";
String regex = "(\\([a-zA-Z0-9=]*\\))";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while(matcher.find()) { // find the next match
System.out.println(matcher.group()); // print the match
}
Output:
(GROUP=test1)
(GROUP=test2)
(GROUP=test3)
(GROUP=test4)

Java regex split string by comma but ignore quotes and also parentheses [duplicate]

This question already has answers here:
Java: splitting a comma-separated string but ignoring commas in quotes
(12 answers)
Closed 9 years ago.
I'm stuck with this regex.
So, I have input as:
"Crane device, (physical object)"(X1,x2,x4), not "Seen by research nurse (finding)", EntirePatellaBodyStructure(X1,X8), "Besnoitia wallacei (organism)", "Catatropis (organism)"(X1,x2,x4), not IntracerebralRouteQualifierValue, "Diospyros virginiana (organism)"(X1,x2,x4), not SuturingOfHandProcedure(X1)
and in the end I would like to get is:
"Crane device, (physical object)"(X1,x2,x4)
not "Seen by research nurse (finding)"
EntirePatellaBodyStructure(X1,X8)
"Besnoitia wallacei (organism)"
"Catatropis (organism)"(X1,x2,x4)
not IntracerebralRouteQualifierValue
"Diospyros virginiana (organism)"(X1,x2,x4)
not SuturingOfHandProcedure(X1)
I've tried regex
(\'[^\']*\')|(\"[^\"]*\")|([^,]+)|\\s*,\\s*
It works if I don't have a comma inside parentheses.
RegEx
(\w+\s)?("[^"]+"|\w+)(\(\w\d(,\w\d)*\))?
Java Code
String input = ... ;
Matcher m = Pattern.compile(
"(\\w+\\s)?(\"[^\"]+\"|\\w+)(\\(\\w\\d(,\\w\\d)*\\))?").matcher(input);
while(matcher.find()) {
System.out.println(matcher.group());
}
Output
"Crane device, (physical object)"(X1,x2,x4)
not "Seen by research nurse (finding)"
EntirePatellaBodyStructure(X1,X8)
not "Besnoitia wallacei (organism)"(X1,x2,x4)
not "Catatropis (organism)"(X1,x2,x4)
not IntracerebralRouteQualifierValue
not "Diospyros virginiana (organism)"(X1,x2,x4)
not SuturingOfHandProcedure(X1)
Don't use regexes for this. Write a simple parser that keeps track of the number of parentheses encountered, and whether or not you are inside quotes. For more information, see: RegEx match open tags except XHTML self-contained tags
Would this do what you need?
System.out.println(yourString.replaceAll(", not", "\nnot"));
Assuming that there is no possibility of nesting () within (), and no possibility of (say) \" within "", you can write something like:
private static final Pattern CUSTOM_SPLIT_PATTERN =
Pattern.compile("\\s*((?:\"[^\"]*\"|[(][^)]*[)]|[^\"(]+)+)");
private static final String[] customSplit(final String input) {
final List<String> ret = new ArrayList<String>();
final Matcher m = CUSTOM_SPLIT_PATTERN.matcher(input);
while(m.find()) {
ret.add(m.group(1));
}
return ret.toArray(new String[ret.size()]);
}
(disclaimer: not tested).

Replacing specail character and it rightmost string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to replace special character and its next string with one string
I have the following String:
"Hello $employee.$currency and $bankbalance"
My task is replacing $ and the following String with another String.
I will get more Strings like this in run time and have to scan and identify anything that starts with $ and should replace with corresponding String.
At run time string with starts with $ should replaced with single string in all it occurences
I will suggest use regex "\$[^. \n\0$]+"
Find the first and last index of the pattern.
Pattern pattern = Pattern.compile("\$[^\. \\n$]+");
Matcher matcher = pattern.matcher(string)
if (matcher.find()) {
start = matcher.start()
end = matcher.end()
text = matcher.group()
}
and replace that part of the string.
String st = "Hello $employee.$currency and $bankbalance";
String pattern = "[$]\\w+";
String res = st.replaceAll(pattern,"mystring");
System.out.println(res);
Output = Hello mystring.mystring and mystring
For Java regex tutorial

Categories