Split a string with regular expression - java

How can I split string for 3 character? (I don't want to do loop for this, maybe some regular expression will be help)
I give example:
String str = "111222333444";
String[] result = str.split("help?"); // get "111", "222", "333"

Using guava-library
Iterable<String> strNums = Splitter.fixedLength(3).split("111222333444")
Readable than using regex. You can then use Ints.tryParse(...) to get Integer version if you want.

Using .split will match regular expressions in the string which, in the underlying implementation, involves traversing the entire string anyway. Writing a simple loop to just create a token from every 3 characters would probably be more efficient.

Frankly, I don't think you can do it for a string of undefined length, without a loop.

You can not use split because the arg of split is the separator, not the resulting sub-strings.
So, your separator regex would be nothing !?
Sorry, you heve write loop. BTW, the regex engine for splitis full of loops.

Related

Unable to Split by ":[" in Java

I have 2 nested HashMaps as a String which I am trying to parse.
My String is as follows :
"20:[cost:431.14, Count:19, Tax:86.228"
Therefore I need to Split by ":[" in order to get my key, 20, For some reason I'm not able to do this.
I have tried :
myString.split(":[") and myString.split("\\:[") but neither seem to work.
Can anyone detect what I have wrong here?
Thanks in Advance
You have to escape the character [ , but not the character : like below:
String str = "20:[cost:431.14, Count:19, Tax:86.228";
String[] spl = str.split(":\\[");
String.split use regex.
Splits this string around matches of the given regular expression.
You need to escape [ since this is a "reserved" character in regular expresionn, not :
myString.split(":\\[")
Not that you could/should set a limit if you only want the first cell
myString.split(":\\[", 2);
This will return an array of 2 cell, so after the first occurence, it doesn't need to read the rest of the String. (This is not really necessary but good to know).
Use Pattern.quote to automatically escape your string
String string = "20:[cost:431.14, Count:19, Tax:86.228";
String[] split = string.split(Pattern.quote(":["));
Another solution :
Therefore I need to Split by ":[" in order to get my key, 20. For
some reason I'm not able to do this.
In this case you can use replaceAll with some regex to get this input so you can use :
String str = "20:[cost:431.14, :[Count:19, Tax:86.228";
String result = str.replaceAll("(.*?):\\[.*", "$1");// output 20
regex demo
If the key is just an integer you can use (\d+):\[ check regex demo
be noted '[' character is special character in regular expression so you have to make an escape character like \\ str.split(":\\["); and remember the string is immutable so if do you want to use it twice you have to reassign it with split like this String[] spl =str.split(":\\[");
Another solution if you just need the key "20" in your String is to substring it to get the part before the delimiter.
String key = myString.substring(0, myString.indexOf(":["));

how to break the string using keywords using regex

I have a scenario where i need to break the below input string based on the keywords using regex.
Keywords are UPRCAS, REPLC, LOWCAS and TUPIL.
String input = "UPRCAS-0004-abcdREPLC-0003-123TUPIL-0005-adf2344LOWCAS-0003-ABCD";
The output should be as follows
UPRCAS-00040-abcd
REPLC-0003-123
TUPIL-0005-adf2344
LOWCAS-00030-ABCD
How can i achieve this using java regex.
I have tried using split by '-' and using regex but both the approach gives an array of strings and again i have to process each string and combine 3 strings together to form UPRCAS-00040-abcd. I felt this is not the efficient way to do as it takes an extra array and process them back.
String[] tokens = input.split("-");
String[] r = input.split("(?=\\p{Upper})");
Please let me know if we can split the string using regex based on the keyword. Basically i need to extract the string between the keyword boundary.
Edited question after understanding the limitation of existing problem
The regex should be generic to extract the string from input between the UPPERCASE characters
The regex should not contains keywords to split the string.
I understood that, it is a bad idea to add new keyword everytime in regex pattern for searching. My expectation is to be a generic as possible.
Thanks all for your time. Really appreciate it.
Split using the following regex:
(?=UPRCAS|REPLC|LOWCAS|TUPIL)
The (?=xxx) is a zero-width positive lookahead, meaning that it matches the empty space immediately preceding one of the 4 keywords.
See Regular-Expressions.info for more information: Lookahead and Lookbehind Zero-Length Assertions
Test
String input = "UPRCAS-0004-abcdREPLC-0003-123TUPIL-0005-adf2344LOWCAS-0003-ABCD";
String[] output = input.split("(?=UPRCAS|REPLC|LOWCAS|TUPIL)");
for (String value : output)
System.out.println(value);
Output
UPRCAS-0004-abcd
REPLC-0003-123
TUPIL-0005-adf2344
LOWCAS-0003-ABCD
You can try this regex:
\w+-\w+-(?:[a-z0-9]+|[A-Z]+)
Demo: https://regex101.com/r/etKBjI/3

Splitting string gives null result in java

i am trying very simple splitting.
I dont know why it is not working.
String abc= "192.168.120.2";
String[] eachByteabc= abc.split(".");
When I debug it and see, I get the result that abc contains : 192.168.120.2.
But when I do split, it does not give me error but gives me null result.
I think, i have made some silly mistake.
Can you tell me where I am wrong. What should I do.
Thank you in advance.
Try it =):
String[] eachByteabc= abc.split("\\.");
You need to escape the ., since it's a regex operator. Change it to:
String[] eachByteabc= abc.split("[.]");
Addition, thanks to #sparks:
While this will work, the [] characters in regex are used to annotate
a set, so if you are looking for where it might be in a limited series
of characters, you should use them.
In this case - use \\. to escape the . character.
public String[] split(String regex) takes a regular expression as an argument. You must escape the point, since it's a regex operator.
String[] eachByteabc = abc.split("."); is not eorr,but you can not to debug and Watch the values.use String[] eachByteabc = abc.split(".");you can Watch values in the debug.
String.split uses regex, so you need to use abc.split("\\.");
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

Java regex pattern for a string

I am new to regex. I am looking for regular expression which matches following pattern and extract the string,
key1=test1
key2="test1" // which will extract test1 stripping quotes
key3=New test
key4=New" "test // which will extract New" "test - as it is if the quotes come in between
I tried with \\s*(\\S+)\\s*=\\s*(\\S+*+) , but not sure how to include quotes if present. Any help will be really appreciated.
Here's a solution without regex to avoid problems with nested quotes:
String extractValue(String input) {
// check if '=' is present here...
String[] pair = input.split("=", 2);
String value = pair[1];
if (value.startsWith("\"") && value.endsWith("\"")) {
return value.substring(1, value.length() - 1);
}
return value;
}
Basically this is not without regex, because of the use of split(), but it's not using regex the way you were planning to use it.
A simple solution would be to just load it as a Properties, which will do exactly the parsing you're looking for. Otherwise, just read each line and split the string at the first "=".
You could use ^([^=]+)=("([^"]*)"|([^"].*))$, but the answer will show up in the third or fourth group depending on if the value was quoted or not so you'd need to check both and pull whichever one was not null.
For Regex, if you want to include " in your regex, simply escape it using \\". Whatever you are trying to achieve, test directly first at http://www.regexpal.com/

regular expression to remove a space

I need a way to remove only the first space found in a string and then put the string in an array. For example
hello there. Hey.
I want that to be split like [hello][there. Hey]. I tried with
String [] s = str.split(" ")
by that will naturally remove all the spaces and create several strings. i just need 2. Can you please tell me how to do that ? Ether by regular expression or another way.
String [] s = str.split (" ", 2); should do the trick, documentation here.
You may also want to consider using \s+ as the regex - it may split the string more intelligently.
Using a regular expression for this isn't necessarily your best option.
Find the first space using position() (whatever the java method is), and then use substring() from the beginning of the string to that position, and again from that position to the end of the string.

Categories