I have the following String:
"location-string:location-string:location-C?:\string"
which I would like to split into the following three Strings:
location-string location-string location-C?:\string
What should the regex expression be when using String.split(regex)?
Basically, I want to split on colon ':' characters except those that are preceded by a '?' character!
Thanks in advance,
PM.
You could use negative lookbehind. It matches the colon which was not preceeded by ?
(?<!\?):
Java regex would be,
"(?<!\\?):"
DEMO
You could use a split() with limit.
public static void main(String[] args) {
String s = "location-string:location-string:location-C?:\\string";
System.out.println(Arrays.toString(s.split(":", 3)));
}
O/P :
[location-string, location-string, location-C?:\string]
Related
The requirement is simple: if the given string matches:
starts with ';'
starts with some char or chars among '\r','\n','\t',' ', and then followed with ';'.
For example ";", "\r;","\r\n;", " \r\n \t;" should all be ok.
Here is my code and it does not work:
private static String regex = "[\\r|\\n| |\\t]+;";
private static boolean startsWithSemicolon(String str) {
return str.matches(regex);
}
Thanks for any help.
You have 2 choices:
Use matches(), in which case the regex must match the entire input, so you'd have to add matching of characters following the ;.
Regex: str.matches("[\\r\\n\\t ]*;.*")
or: Pattern.compile("[\\r\\n\\t ]*;.*").matcher(str).matches()
Use find(), in which case the regex must be anchored to the beginning of the input:
Regex: Pattern.compile("^[\\r\\n\\t ]*;").matcher(str).find()
I am trying to work out if there is a way to get a check to ensure the string I am checking follows a structure.
eg: String s = "abcd, afsfsfs, abcdef, 90> 20, abeds"
Need to confirm that there is a ', ' followed by a ', ' followed by a ', 'followed by a '> ' and finally a ', '. The letters and numbers can vary in length between the characters that separate them.
I am a bit stuck on this. Any help would be appreciated.
If you want any number of letters,digits between special characters you can use this regex:
public static void main(String[] args) {
String s = "abcd, afsfsfs, abcdef, 90> 20, abeds";
boolean matches = s.matches("\\w+, \\w+, \\w+, \\d+> \\d+, \\w+");
System.out.println(matches);
}
You can use the following regex pattern in conjunction with String#matches():
.*, .*, .*, .*>.*, .*
Code sample:
public static void main(String args[])
{
String s = "abcd, afsfsfs, abcdef, 90> 20, abeds";
if (s.matches(".*, .*, .*, .*>.*, .*")) {
System.out.println("match");
}
else {
System.out.println("no match");
}
}
Demo here:
Rextester
Try something like the below.FYI,not tested yet. Explanation, With [^,]+ .. you are saying match anything but , and then match ,. The second pattern is [^>]+ > Match any char but > and the match >.
[] Character Classes or Character Sets
^ inside [ ] means Negated Character Classes. read more
^(?![\s]*$) [^,]+ , [^,]+ , [^,]+ , [^>]+ > [^,]+ $
start no empty 1st 2nd 3rd 4th end
Try this:
^\s*(?:\s*\w+\s*,\s*){3}\w+\s*>\s*\w+,(?!.*[,>]).*$
Regex Demo 1
it will make sure that the format is exactly what you have wanted. and there is no further , or > sign in the rest of the string. But if your intention is to allow more repetition of ,> in the string once the format is being found, then you may remove the next to last part i.e. (?!.*[,>]) from the regex thus it becomes:
^\s*(?:\s*\w+\s*,\s*){3}\w+\s*>\s*\w+,.*$
Regex Demo 2
I want a java regex that could convert this simple sentence like this:
1) I am a (happy) person!!!
into
I am a happy person.
i.e. ignore all the numbers and special characters but ignore the white space between the words.
I am using this regex right now:
("\\P{L}", "")
but it is giving a output like:
Iamahappyperson
Thanks for your help!!
Use this regex [^\p{Alpha} ]
\p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}]
[^\p{Alpha} ] All character except alphabetic and space
Here is how you can use this :
System.out.println("1) I am a (happy) person!!!".replaceAll("[^\\p{Alpha} ]", ""));
If you want to keep unicode alphabet character add just add (?U) at the start of the above regex.Here (?U) turn on UNICODE_CHARACTER_CLASS flag
System.out.println("I am a (happy) person!!!".replaceAll("[^\\p{L}\\p{Z}]", ""));
prints out "I am a happy person"
Try using this methode
String new_string = "I am a (happy) person!!!".replaceAll("[^a-zA-Z ]+","");
The code:
public class HelloWorld
{ public static void main(String[] args)
{
String new_string = "I am a (happy) person!!!".replaceAll("[^a-zA-Z ]+","");
System.out.print(new_string);
}
}
The output: I am a happy person
[a-zA-Z\d\s] if you want to keep alphanumeric and space.
[a-zA-Z\s] for alpha and space only.
Why is non-greedy match not working for me? Take following example:
public String nonGreedy(){
String str2 = "abc|s:0:\"gef\";s:2:\"ced\"";
return str2.split(":.*?ced")[0];
}
In my eyes the result should be: abc|s:0:\"gef\";s:2 but it is: abc|s
The .*? in your regex matches any character except \n (0 or more times, matching the least amount possible).
You can try the regular expression:
:[^:]*?ced
On another note, you should use a constant Pattern to avoid recompiling the expression every time, something like:
private static final Pattern REGEX_PATTERN =
Pattern.compile(":[^:]*?ced");
public static void main(String[] args) {
String input = "abc|s:0:\"gef\";s:2:\"ced\"";
System.out.println(java.util.Arrays.toString(
REGEX_PATTERN.split(input)
)); // prints "[abc|s:0:"gef";s:2, "]"
}
It is behaving as expected. The non-greedy match will match as little as it has to, and with your input, the minimum characters to match is the first colon to the next ced.
You could try limiting the number of characters consumed. For example to limit the term to "up to 3 characters:
:.{0,3}ced
To make it split as close to ced as possible, use a negative look-ahead, with this regex:
:(?!.*:.*ced).*ced
This makes sure there isn't a closer colon to ced.
I have tried the following example but it gives following out put
output[]. I have pass the string "1.0" to function calculatePayout() and want to store the 1 in s[0] and 0 in s[1]
import java.util.Arrays;
public class aps {
public void calculatePayout(String amount)
{
String[] s = amount.split(".");
System.out.println("output"+Arrays.toString(s));
}
public static void main(String args[])
{
new aps().calculatePayout("1.0");
}
}
Method split() accepts regular expression. Character . in regular expressions means "everything". To split your string with . you have to escape it, i.e. split("\\."). The second back slash is needed because the first one escapes dot for regular expression, the second escapes back slash for java compiler.
Try escaping the dot:
String[] s = amount.split("\\.");
As dot is "Any character" in regex.
Try amount.split("\\.")
Split method uses a regex so you need to use the correct syntax and escape the dot.
. is a metacharcter or special character in regex world. String#split(regex) expects regex as parameter, you either have to escape it with backslash or use character class in-order to treat it as a normal character
Either amount.split("\\."); or amount.split("[.]");
Try
amount.split("\\.")
split method accepts a regular expression