How to Validate a String with Colon Restriction [closed] - java

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

Related

What will be the regex for the given case? [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 years ago.
Improve this question
I'm working on a regex for getting a specific number pattern from the URL string.
Requirements: Desire string should start from - or /, followed by a sequence of digits and ending with a / or nothing.
I tried: [-\/](\d+)(\/|$), but for e.g. in www.abc.com/pages/Toms-1777/14623420046 I want /14623420046(i.e. the second occurring digit sequence), but according to my regex, the result will be -1777/. I was trying negative lookbehind but not able to make any progress. I'm new to all this. Please guide.
Test cases: (with matched pattern)
www.abc.com/pages/Essen-Massage-Therapy-LLC/130561253629638
www.abc.com/biz/finn-mccools-santa-monica-2
www.abc.com/summerset.gardens.7
www.abc.com/pages/Toms-1777/14623420046
www.abc.com/pages/The-Clean-Masters/1403753595526512
www.abc.com/24hfsheepsheadbay
www.abc.com/sample2NVCoolSpace
www.abc.com/pages/Jet-Set-3920/542495615847409
www.abc.com/temp.buildings.77
www.abc.com/2423423453534temp/2312312312312312312
www.abc.com/Ptemp-Gtemp-Dtemp-189398324428792/temp
You want that $ in either case. Instead of 'slash OR end', it's more 'optional slash and then a very much not-optional end'. So.. /?$. You don't need to normally escape slashes, especially in java regexes:
Pattern.compile("[-/](\\d+)/?$")
This reads: From minus or slash, a bunch of digits, then 0 or 1 slashes, then the end. Note, use find() and not matches() - matches only works if the entire string matches, which it won't, as the - or / occurs halfway through.
EDIT: Was missing a backslash in the java string.

String a[] = s.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); in java [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 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.

Simple Regular expression Java, brackets and doubles [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 6 years ago.
Improve this question
This is very basic but can't seem to find a answer when searching.
I have a string that's in the format:
[[[0.093493,51.6037],[0.091015,51.5956],[0.088596,51.5857]]]
The doubles inside the brackets are [latitude coordinate,longitude coordinate].
From this I'd like to extract the coordinates.
What should I put inside the search pattern if I use a Pattern/Matcher solution?
Assume that format with brackets is always correct but the doubles can vary in length.
Basicly what I want the code to do is:
Find "[" left of a number, then find this "," and return what's in between
AND another searchpattern that:
find "," and "]" and return what's inbetween.
Keep it simple by using this regex:
\[(\d+\.\d+),(\d+\.\d+)\]
and repeat the matcher.find() till all matches are found.
You matches are in group #1 and group #2
RegEx Demo

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

How this regular expression do this? [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 8 years ago.
Improve this question
I want to use below expression in my program but i don't know what do this regular expression!
please help me.
"(?=(?!^)[,;.:])|(?<=[,;.:])"
in the above expression (?=(?!^)[,;.:]) find any character set that end with [.;,:] or no? what do this (?!^) in this expression?
and this expression find any character set that end with [,;.:] or no?
please help me.
The expression matches 0-length strings that satisfy one of these two conditions:
Ahead of it is one of ,;.:, but not for 0-length strings just before the beginning of the subject string (position 0). So the subject string "." has no match at position 0, only at position 1 because of the following alternative. This is done with positive lookahead (?=) and negative lookahead (?!).
Behind it is one of ,;.:. This is done with positive lookbehind (?<=).
For instance for "aaa,1", you have two matches: at position three (after the last a, because it's followed by ,) and at position 4 (because it's preceded by ,).

Categories