Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following string
String names= D:45454546544654 A:45454545454 C:454545474
I need Output to be
String data[]=[45454546544654,45454545454,454545474]
First replace all the uppercase letter plus the following colon : with an empty string and then split the resultant string according to the spaces.
String names = "D:45454546544654 A:45454545454 C:454545474";
String parts[] = names.replaceAll("[A-Z]:", "").split("\\s+");
System.out.println(Arrays.toString(parts));
Output:
[45454546544654, 45454545454, 454545474]
Rather than split you can just match:
(?<=:)\d+(?!\d)
Using this Pattern:
Pattern p = Pattern.compile("(?<=:)\\d+(?!\\d)");
Then you can use Matcher.find API to get all the matches.
RegEx Demo
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to find the regex to get the last letter in a string:
String str = "A76B62Z**F**63";
Finding last letter via regex should return 'F'.
You can greedily match any sequence of characters before a letter:
String s = "A76B62ZF63";
Matcher m = Pattern.compile(".*([A-Za-z])").matcher(s);
if(m.find()) System.out.println(m.group(1));
With Java 9+:
String s = "A76B62ZF63";
Pattern.compile(".*([A-Za-z])").matcher(s).results()
.findFirst().ifPresent(r -> System.out.println(r.group(1)));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've got a string like this:
1454974419:1234;1454974448:3255,2255,66789
I would like to extract/group these values by using regular expression in java.
1234
3255225566789
You can use this lookbehind and negation based regex:
(?<=[:,])[^;,]+
RegEx Demo
Breakup:
(?<=[:,]) # lookbehind to assert if previous char is : or ,
[^;,]+ # match 1 or more of anything that is not a ; or ,
Try this
String yourString= "1454974419:1234;1454974448:3255,2255,66789";
Pattern myPattern = Pattern.compile("[^a-zA-Z0-9]");
Matcher myMatcher = myPattern.matcher(yourString);
while(myMatcher.find())
{
String temp= myMatcher.group();
yourString=yourString.replaceAll("\\"+temp, "");
}
System.out.println(yourString);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i have a string like:
String test = "firstName:a,lastName:b,addressOne:line 1,line 2,city:other";
i am trying to do test.replaceAll(",","\",\"").I mean replace , with ",". I want to this only for the whole strings. For eg addressOne is a single string with comma seperated i dont want to replace that. Is there any regex or someother way i can do this?
i should get a string like
"firstName":"a","lastName":"b","addressOne":"line 1 , line 2","city":"other"
after replace , but i am getting
"firstName":"a","lastName":"b","addressOne":"line 1 "," line 2","city":"other".
You can split your string with commas that are followed by a word which has been followed by :. And for this aim you can use a positive look-ahead and for matching the leading word use a negated character class [^:,]+ which will match any string except : and ,:
test.split(",(?=[^:,]+:)")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to take out all the characters of a string that are not numbers.
You could use a Character Class Intersection like [\p{Punct}\p{Lower}\p{Upper}&&[^.]]
But why not just use
[^\d.]+
As Java String "[^\\d.]+"
This would match one or more characters, that are not \d a digit or the . period.
I'd suggest using \\d+ then (it's consecutive digits), and a capture group. Something like
String str = "";
str = str.replaceAll("(\\d+\\.\\d+)", "$1");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having one string containing "This is a time to get involve into $FO, $RTP, $DFG and $RG"
Use following regular expression:
"\\$\\w+"
$ should be escaped.
\w match digits, alphabet, _.
If you need only match alphabets, use [a-zA-Z] instead.
This will work too
String str="This is a time to get involve into $FO, $RTP, $DFG and $RG" ;
String[] arr=str.split(" ");
for (String i:arr){
if(i.indexOf("$")==0){
System.out.println(i.replaceAll("\\,",""));
}
}