java regex help - java

I have a string in the following format, I only need to extract the /jspFolderTestSecondLast/jspFolderTestLast,
which is the second last seperated by /.
www.name.com/jspFolderTestOne/jspFolderTestTwo/jspFolderTestAndmanyMore/jspFolderTestSecondLast/jspFolderTestLast
/jspFolderTestSecondLast/jspFolderTestLast can be varied in length but always gonna be separated by secong last /.
Any help is appreciated.
Thanks

String s = "www.name.com/jspFolderTestOne/jspFolderTestTwo/jspFolderTestAndmanyMore/jspFolderTestSecondLast/jspFolderTestLast"
String[] parts = s.split("/");
String whatYouWant = parts[parts.length-2] +"/" + parts[parts.length-1]

You don't need any regexes for that, since you can just split the string on '/' and get two last array indexes. But here's the regex anyway:
^.+(/[^/]+)(/[^/]+)$
$1 contains the first and $2 contains the second block

String str = "www.name.com/jspFolderTestOne/jspFolderTestTwo/jspFolderTestAndmanyMore/jspFolderTestSecondLast/jspFolderTestLast";
String are[] = str.split("/");//may be you need to add escape here
//take last two parts

Pattern p = Pattern.compile(".*(/[^/]+/[^/]+)$");
Matcher m = p.matcher("a/b/c/d.txt");
if( m.matches() ) {
System.out.println(m.group(1));
}

this is the javascript version:
"www.name.com/jspFolderTestOne/jspFolderTestTwo/jspFolderTestAndmanyMore/jspFolderTestSecondLast/jspFolderTestLast".search(/\/[^\/]*\/[^\/]*$/)
or you can group them nicely:
(/(\/[^\/]*)(\/[^\/]*)$/)

Related

Need Regex to replace all characters between first set of parenthesis from a string

I've been able to generate a regex to pull everything that is between parenthesis in a string, but I'm unclear on how to make it only happen once and only with the first set. In JAVA:
My current pattern = "\\(([^)]+)\\)"
Any help would be greatly appreciated.
Use replaceFirst instead of replaceAll
OR if you must use replaceAll let it consume rest of your string and put it back again like
replaceAll("yourRegex(.*)","yourReplacement$1");
where $1 represents match from first group (.*).
try:
String x= "Hie(Java)";
Matcher m = Pattern.compile("\\((.*?)\\)").matcher(x);
while(m.find()) {
System.out.println(m.group(1));
}
or
String str = "Hie(Java)";
String answer = str.substring(str.indexOf("(")+1,str.indexOf(")"));
for last index:
update with
String answer = str.substring(str.indexOf("(")+1,str.lastIndexOf(")"));

Spilt string and match end of string with enum values

I have one string which i need to divide into two parts using regex
String string = "2pbhk";
This string i need to divide into 2p and bhk
More over second part should always be bhk or rk, as strings can be one of 1bhk, 5pbhk etc
I have tried
String pattern = ([^-])([\\D]*);
You can use the following regex "(?=bhk|rk)" with split.
str.split("(?=bhk|rk)");
This will split it if there is one of bhk or rk.
This should do the trick:
(.*)(bhk|rk)
First capture holds the "number" part, and the second bhk OR rk.
Regards
String string = "2pbhk";
String first_part, second_part = null;
if(string.contains("bhk")){
first_part = string.substring(0, string.indexOf("bhk"));
second_part = "bhk";
}
else if(string.contains("rk")){
first_part = string.substring(0, string.indexOf("rk"));
second_part = "rk";
}
Try the above once, not using regex but should work.
In case you are looking to split strings that end with rk or bhk but not necessarily at the end of the string (i.e. at the word boundaries), you need to use a regex with \\b:
String[] arr = "5ddddddpbhk".split("(?=(?:rk|bhk)\\b)");
System.out.println(Arrays.toString(arr));
If you want to allow splitting inside a longer string, remove the \\b.
If you only split individual words, use $ instead of \\b (i.e. end of string):
(?=(?:rk|bhk)$)
Here is my IDEONE demo

How to substring a string to the second dot (.) in Java?

I have a String which has many segments separated by a dot (.) like this:
codes.FIFA.buf.OT.1207.2206.idu
I want to get a substring only until second dot, like codes.FIFA.
How to substring just until the second dot?
Just find the first dot, then from there the second one:
String input = "codes.FIFA.buf.OT.1207.2206.idu";
int dot1 = input.indexOf(".");
int dot2 = input.indexOf(".", dot1 + 1);
String substr = input.substring(0, dot2);
Of course, you may want to add error checking in there, if dots are not found.
Something like this will do the trick:
String[] yourArray = yourDotString.split(".");
String firstTwoSubstrings = yourArray[0] + "." + yourArray[1];
The variable firstTwoSubstrings will contain everything before the second ".". Beware that this will cause an exception if there are less than two "." in your string.
Hope this helps!
Matcher m = Pattern.compile("^(.*?[.].*?)[.].*")
.matcher("codes.FIFA.buf.OT.1207.2206.idu");
if (m.matches()) {
return m.group(1);
}
http://ideone.com/N6m8a
This seems like the easiest solution:
String[] split = "codes.FIFA.buf.OT.1207.2206.idu".split("\\.");
System.out.println(split[0] + "." + split[1]);
I'd just split it into three parts and join the first two again:
String[] parts = string.split("\\.", 3);
String front = parts[0]+"."+parts[1];
String back = parts[2];
This may need some error checking if it can have less than two dots, or start with a dot, etc.

Help in writing a Regular expression for a string

Hi please help me out in getting regular expression for the
following requirement
I have string type as
String vStr = "Every 1 nature(s) - Universe: (Air,Earth,Water sea,Fire)";
String sStr = "Every 1 form(s) - Earth: (Air,Fire) ";
from these strings after using regex I need to get values as "Air,Earth,Water sea,Fire" and "Air,Fire"
that means after
String vStrRegex ="Air,Earth,Water sea,Fire";
String sStrRegex ="Air,Fire";
All the strings that are input will be seperated by ":" and values needed are inside brackets always
Thanks
The regular expression would be something like this:
: \((.*?)\)
Spelt out:
Pattern p = Pattern.compile(": \\((.*?)\\)");
Matcher m = p.matcher(vStr);
// ...
String result = m.group(1);
This will capture the content of the parentheses as the first capture group.
Try the following:
\((.*)\)\s*$
The ending $ is important, otherwise you'll accidentally match the "(s)".
If you have each string separately, try this expression: \(([^\(]*)\)\s*$
This would get you the content of the last pair of brackets, as group 1.
If the strings are concatenated by : try to split them first.
Ask yourself if you really need a regex. Does the text you need always appear within the last two parentheses? If so, you can keep it simple and use substring instead:
String vStr = "Every 1 nature(s) - Universe: (Air,Earth,Water sea,Fire)";
int lastOpeningParens = vStr.lastIndexOf('(');
int lastClosingParens = vStr.lastIndexOf(')');
String text = vStr.substring(lastOpeningParens + 1, lastClosingParens);
This is much more readable than a regex.
I assume that there are only whitespace characters between : and the opening bracket (:
Pattern regex = Pattern.compile(":\\s+\\((.+)\\)");
You'll find your results in capturing group 1.
Try this regex:
.*\((.*)\)
$1 will contain the required string

Use String.split() with multiple delimiters

I need to split a string base on delimiter - and .. Below are my desired output.
AA.BB-CC-DD.zip ->
AA
BB
CC
DD
zip
but my following code does not work.
private void getId(String pdfName){
String[]tokens = pdfName.split("-\\.");
}
I think you need to include the regex OR operator:
String[]tokens = pdfName.split("-|\\.");
What you have will match:
[DASH followed by DOT together] -.
not
[DASH or DOT any of them] - or .
Try this regex "[-.]+". The + after treats consecutive delimiter chars as one. Remove plus if you do not want this.
You can use the regex "\W".This matches any non-word character.The required line would be:
String[] tokens=pdfName.split("\\W");
The string you give split is the string form of a regular expression, so:
private void getId(String pdfName){
String[]tokens = pdfName.split("[\\-.]");
}
That means to split on any character in the [] (we have to escape - with a backslash because it's special inside []; and of course we have to escape the backslash because this is a string). (Conversely, . is normally special but isn't special inside [].)
Using Guava you could do this:
Iterable<String> tokens = Splitter.on(CharMatcher.anyOf("-.")).split(pdfName);
For two char sequence as delimeters "AND" and "OR" this should be worked. Don't forget to trim while using.
String text ="ISTANBUL AND NEW YORK AND PARIS OR TOKYO AND MOSCOW";
String[] cities = text.split("AND|OR");
Result : cities = {"ISTANBUL ", " NEW YORK ", " PARIS ", " TOKYO ", " MOSCOW"}
pdfName.split("[.-]+");
[.-] -> any one of the . or - can be used as delimiter
+ sign signifies that if the aforementioned delimiters occur consecutively we should treat it as one.
I'd use Apache Commons:
import org.apache.commons.lang3.StringUtils;
private void getId(String pdfName){
String[] tokens = StringUtils.split(pdfName, "-.");
}
It'll split on any of the specified separators, as opposed to StringUtils.splitByWholeSeparator(str, separator) which uses the complete string as a separator
String[] token=s.split("[.-]");
It's better to use something like this:
s.split("[\\s\\-\\.\\'\\?\\,\\_\\#]+");
Have added a few other characters as sample. This is the safest way to use, because the way . and ' is treated.
Try this code:
var string = 'AA.BB-CC-DD.zip';
array = string.split(/[,.]/);
You may also specified regular expression as argument in split() method ..see below example....
private void getId(String pdfName){
String[]tokens = pdfName.split("-|\\.");
}
s.trim().split("[\\W]+")
should work.
you can try this way as split accepts varargs so we can pass multiple parameters as delimeters
String[]tokens = pdfName.split("-",".");
you can pass as many parameters that you want.
If you know the sting will always be in the same format, first split the string based on . and store the string at the first index in a variable. Then split the string in the second index based on - and store indexes 0, 1 and 2. Finally, split index 2 of the previous array based on . and you should have obtained all of the relevant fields.
Refer to the following snippet:
String[] tmp = pdfName.split(".");
String val1 = tmp[0];
tmp = tmp[1].split("-");
String val2 = tmp[0];
...

Categories