Splitting a string into equals parts (java/groovy) [duplicate] - java

This question already has answers here:
Split string to equal length substrings in Java
(23 answers)
Closed 6 years ago.
I'd like to split the string into substrings which has 20 chars (or less for the tail). Is there some library or I need to make the class for that?

you should use :
s.split("(?<=\\G.{20})");
\G is a zero-width assertion that matches the position where the previous match ended. If there was no previous match, it matches the beginning of the input, the same as \A. The enclosing lookbehind matches the position that's 20 characters along from the end of the last match.

Or, with Groovy you could do:
assert 'abcdefghij'.toList().collate( 3 )*.join() == ['abc', 'def', 'ghi', 'j']

Related

RegEx ignore letters until digit occurs and then match letters afterwards [duplicate]

This question already has answers here:
Using Regular Expressions to Extract a Value in Java
(13 answers)
Closed 3 years ago.
This is intended to be used in Java.
Imagine following sample input:
WRA1007
1085808
1092650S
3901823CV
I want to match all alphabetic characters after at least one digit.
Desired output:
S
CV
Actual output:
0S
3CV
My current approach looks like this:
\d[a-zA-Z]+
The problem with this pattern is that it includes the digit beforehand too. My current solution is to remove the first character of the resulting string afterwards. And this seems quite unsatisfactory to me.
You need a lookbehind:
(?<=\d)[a-zA-Z]+
(?<=\d) means "there must be a digit before this position, but don't match it".
Demo
Alternatively, you can use a pair of () to surround the part you want to get:
\d([a-zA-Z]+)
This is called a "group", and you can get its value by calling group(1) on your Matcher.
If you 'add' groups you can get group 1 that contain only letters
\d([a-zA-Z]+)

Java regex for consecutive exponential terms [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regex to first occurrence only? [duplicate]
(4 answers)
Closed 4 years ago.
I've tested this regex:
String regex = "[e][#](.)+[$]
the regex works well to identify exponential terms, however it breaks when there are two or more consecutive exponential terms.
I test the regex with the usual code:
while(matcher.find()){
String string = matcher.group();
System.out.println("this one: "+string);
}
When I type the expression:
e#x$ + 3e#x+1$
string equals to (e#(x$+3e#x+1$))
By the way, I added the parentheses inside the while loop. They are necessary for
what I am trying to accomplish.
I want the result of string to be (e(#x$))+3(e(#x+1$)
I know the problem lies in "(.)+ What I think is happening is that the regex
includes the first $, what I need is for it to stop at the first $.
How can I include this logic inside the regex?
thank you

Find repeating characters in a string using regex [duplicate]

This question already has answers here:
Regular expression to match any character being repeated more than 10 times
(8 answers)
Closed 4 years ago.
I'm trying to find a regex which finds 3 repeating characters appearing contiguously in a string. The character set could be alphabet, digit or any special character.
I wanted the first try for alphabet and digits and then extend the expression to include special characters. The ones I tried are. Both of these fail for the string "c2sssFg1". What am I doing wrong here?
(\\w*)\\2{3,}(\\w*)
(\\w*?)(\\w)\\2{3,}(\\w*)
I looked at some of the examples on SO and on web but I didn't find the right solution that passes the random strings I test.
Can someone help me with this?
Thanks.
(.)\1{2}
(.) matches any char
\1 matches that exactly char
{2} is to grant its 2 more of that
Try (.)\1\1. It works for general case.

Regex find digits after String [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 6 years ago.
From the two urls
https://twitter.com/realDonaldTrump/status/829684271812067328
https://twitter.com/realDonaldTrump/status/829684271812067328/
I want to extract the String 829684271812067328 in Java.
My attempt was
\\/status\\/([\\d]+)
but this does not allow anything before /status or after the digits (/).
Whats the solution to this?
If you just need to extract data (rather than validate the url format), I'd use the following regex :
(\\d+)/?$
And extract the first group of the result.
It matches a non-empty sequence of digits that can be followed by a / and must be found at the end of the matched text.

Regex to identify more than 1 occurance of underscore consequtively [duplicate]

This question already has answers here:
Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
(32 answers)
Closed 6 years ago.
I have string in which more than one underscore can be present together.
For example : this_is__Dummy_____String
I have to replace this more than one occurance by only one underscore so that target string should look like :
this_is_Dummy_String
Thanks in advance !
You can use String#replaceAll to replace the undescores.
"this_is__Dummy_____String".replaceAll("_{2,}", "_")
The given regex will replace all occurences of "two or more" underscores with a single underscore.

Categories

Resources