Changing string with comma separated values to numbered new-line values
For example:
Input: a,b,c
Output:
1.a
2.b
3.c
Finding it hard to change it using regex pattern, instead of converting string to string array and looping through.
I'm not really sure, that it's possible to achive with only regex without any kind of a loop. As fore me, the solution with spliting the string into an array and iterating over it, is the most straightforward:
String value = "a,b,c";
String[] values = value.split(",");
String result = "";
for (int i=1; i<=values.length; i++) {
result += i + "." + values[i-1] + "\n";
}
Sure, it's possible to do without splitting and any kind of arrays, but it could be a little bit awkward solution, like:
String value = "a,b,c";
Pattern pattern = Pattern.compile("[(^\\w+)]");
Matcher matcher = pattern.matcher(value.replaceAll("\\,", "\n"));
StringBuffer s = new StringBuffer();
int i = 0;
while (matcher.find()) {
matcher.appendReplacement(s, ++i + "." + matcher.group());
}
System.out.println(s.toString());
Here the , sign is replaced with \n new line symbol and then we are looking for a groups of characters at the start of every line [(^\\w+)]. If any group is found, then we are appending to the start of this group a line number. But even here we have to use a loop to set the line number. And this logic is not as clear, as the first one.
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
I want to split a string based on text qualifier for example
"1","10411721","MikeTison","08/11/2009","21/11/2009","2800.00","002934538","051","New York","10411720-002",".\Images\b.jpg",".\RTF\b.rtf"
Qualifer="
Spliter = ,
I want to split string based on Spliter , but if Spliter comes inside qualifier " than ignore it and return string including Spliter .
Regular expression i am using is (?:|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)
but this regular expression only returns commas,please help me in this perspective as i am new to regular expressions
please note that if we have newline characters in string ie \r\n than it should ignore newline character
"1","10411","Muis","a","21/11/2009","2800.06","0029683778","03005136851","Awan","10411720-001",".\Images\a.jpg",".\RTF\a.rtf"
"2","08/10/2009","07:32","Call","On-Net","030092343242342376543","Monk","00:00","1.500","0.000","10.000","0.200"
"2","08/10/2009","02:50","Call","Off-Net","030092343242342376543","Une","08:00","1.500","2.000","20.000","3.500"
"2","09/10/2009","03:55","SMS","On-Net","030092343242342376543","Mink","00:00","1.500","0.000","5.000","100.500"
"2","09/10/2009","12:30","Call","Off-Net","030092343242342376543","Zog","01:01","3.500","3.000","70.000","6.500"
"2","09/10/2009","09:11","Call","On-Net","030092343242342376543","Monk","02:30","2.00","2.000","90.000","4.000"
Probably easiest solution is not searching for place to split, but finding elements which you want to return. In your case these elements
starts "
ends with "
have no " inside.
So you try with something like
String data = "\"1\",\"10411721\",\"MikeTison\",\"08/11/2009\",\"21/11/2009\",\"2800.00\",\"002934538\",\"051\",\"New York\",\"10411720-002\",\".\\Images\\b.jpg\",\".\\RTF\\b.rtf\"";
Pattern p = Pattern.compile("\"([^\"]+)\"");
Matcher m = p.matcher(data);
while(m.find()){
System.out.println(m.group(1));
}
Output:
1
10411721
MikeTison
08/11/2009
21/11/2009
2800.00
002934538
051
New York
10411720-002
.\Images\b.jpg
.\RTF\b.rtf
You can split using this regex:
String[] arr = input.split( "(?=(([^\"]*\"){2})*[^\"]*$),+" );
This regex will split on commas if those are outside double quotes by using a lookahead to make sure there are even number of quotes after a comma.
Remove the first and the last character of the whole string. Then split with ","
String test = "\"1\",\"10411721\",\"MikeTison\",\"08/11/2009\",\"21/11/2009\",\"2800.00\",\"002934538\",\"051\",\"New York\",\"10411720-002\",\".\\Images\\b.jpg\",\".\\RTF\\b.rtf\"";
if (test.length() > 0)
test = test.substring(1, test.length()-1);
System.out.println(Arrays.toString(test.split("\",\"")));
This works even if you have new line character..try it out
String str="\"1\",\"10411721\",\"MikeTison\",\"08/11/2009\",\"21/11/2009\",\"2800.00\",\"002934538\",\"051\",\"New York\",\"10411720-002\",\".\\Images\\b.jpg\",\".\\RTF\\b.rtf\"";
System.out.println(Arrays.toString(str.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)")));
In Java trying to find a regular expression that will match all instances of a specific character (:) except the first instance, want to replace all instances except first with nothing.
I can do this,
Pattern p = Pattern.compile(":");
Matcher m = p.matcher(input);
String output = m.replaceAll("");
and there is also m.replaceFirst() but I want to replace everything but first.
Naive approach:
String[] parts = str.split(":", 2);
str = parts[0] + ":" + parts[1].replaceAll(":", "");
For regex replace use match pattern \G((?!^).*?|[^:]*:.*?): and as replacement use first group $1
See and test the regex code in Perl here.
public static void main(String[] args) {
String name ="1_2_3_4_5";
int index = name.indexOf("_");
String name1 = name.substring(index+1);
name1 = name1.replace("_", "#");
System.out.println(name.substring(0,index+1)+ name1);
}
You can use reg ex
String str1 = "A:B:C:D:E:F:G:H:I:J:K:L:M";
str1= str1.replaceAll("([:|_].*?):", "$1_");
str1= str1.replaceAll("([:|_].*?):", "$1_");
Here I cant modify the regex to have output in first replace itself. Actually first replaceAll do replace ':' with '_' in alternate positions.
if (matcher.find()) {
String start = originalString.substring(0, matcher.end());
matcher.reset(originalString.substring(matcher.end(), originalString.length()));
replacedString = start + matcher.replaceAll("");
}
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:
(/(\/[^\/]*)(\/[^\/]*)$/)