String a[] = s.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); in java [closed] - java

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 5 years ago.
Improve this question
I am confuse in the logic behind the code (?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)")
it is separating numbers and alphabets like input String abc12dc23 then it is spliting it as output abc 12 dc 23.
I just want the explanation how the above code is working?

This regex:
(?<=\D)(?=\d)|(?<=\d)(?=\D)
matches 2 kinds of patterns, as suggested by the | character:
This pattern:
(?<=\D)(?=\d)
and this pattern:
(?<=\d)(?=\D)
The former looks for a position in the string where there is a non-digit (\D) character before that position and a digit (\d) after it. The latter looks for a position where the reverse happens, a digit before and a non-digit after.
To say this in a more abstract way, the regex is looking for digit-non-digit boundaries.
The split method looks for all occurrences of the pattern and splits the string when it finds one.

Related

Check if word alternates consonant and vowel [closed]

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 3 months ago.
Improve this question
So I need to check if a word is a pattern of alternating vowel and cosonant (Or consonant and vowel) in Java.
I want to make it a regex but I just came with this incomplete regex expression:
[aeiouAEIOI][^aeiouAEIOI]
Any ideas?
Thanks :)
Update: It's not regex restricted, so it can be an option if anyone has any ideas
One way is using a lookahead to check if neither two vowels nor two consonants next to each other.
(?i)^(?!.*?(?:[aeiou]{2}|[^aeiou]{2}))[a-z]+$
See this demo at regex101 (used i flag for caseless matching, the \n in demo is for staying in line)
Update: Thank you for the comment #Thefourthbird. For matching at least two characters you will need to change the last quantifier: Use [a-z]{2,} (two or more) instead of [a-z]+ (one or more). For only matching an even amount of characters (2,4,6,8...), change this part to: (?:[a-z]{2})+
FYI: If you use this with matches you can drop the ^ start and $ end anchor (see this Java demo).

How to Validate a String with Colon Restriction [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to write a boolean function to support double colons :: only for a string. It should reject any string with non-consecutive colon or more than two consecutive colons. The appearance of double colons can be any number. I can write regex which supports double colons but I don't know how to reject so many combinations of non-consecutive and consecutivec colons. Any idea is appreciated!
Valid inputs: Customer::Table, Customer::Table::Sub
Invalid inputs: Customer:Table, Customer::Table:Sub, Customer::::Table
Here is one line option using String#matches:
String input = "Customer::Table::Sub";
if (input.matches("[^:]+(?:::[^:]+)*")) {
System.out.println("MATCH");
}
Demo
Here is an explanation of the regex used:
[^:]+ match one or more non colon characters
(?: start non capturing group
::[^:]+ match :: again followed by one or more non :
)* the group occurring zero or more times

Splitting a comma-separated string but ignoring commas in whole word [closed]

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(",(?=[^:,]+:)")

Is there a way to use \p{Punct}\p{Lower}\p{Upper} and in a regex(java), but without the "." character? [closed]

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");

Regex java need some tips [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I have to use Regex for the name of a character, where the name has to start with a capital and can only exist out of letters, apostrophe's and spaces.
And I dont know how to start, could anyone help me with this?
I suppose your character name is stored in a String variable. For that you can use the String.matches() function, which accepts a regex String parameter.
To create the required regex you will have to combine the folowing:
[A-Z] for the capital letter
[a-z' ]+ for the remaining characters.
Note that when using those in Java you'll need to add some escape characters
You can experiment with regular expressions here: http://www.regexr.com

Categories