Regular expression that matches only up to 10 decimal places [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 5 years ago.
Improve this question
so I'm trying to write a regular expression that can match the following scenarios.
12.1234 = match
112.12345678901 = wont match
1287729918192.123 = match
123927678281818.19883748383839 = won't match
23.1829aga = won't match
1722ahh.98822 = won't match
1.#$122 = won't match
Basically it should only match strings that contain no letters or special characters and values with less than 10 decimal places.
I'm really new to regular expression and am not sure how to accomplish this.
Thank you for any help!

Trick to writing simple regexes:
describe your pattern in words
look for quantifiers, character classes and/or other tokens that satisfy your description
combine all the tokens you found into one line!
Basically, your pattern can be described like this:
start of the string
1 to unlimited number of digits
a dot
between 1 and 10 digits
end of the string
We just translate the above descriptions into regex:
^
\d+
\.
\d{1,10}
$
And combine all these:
^\d+\.\d{1,10}$

This should work:
^\d*\.\d{1,10}\s
To deconstruct this a little bit:
"^" means starting at the start of the line of text
"\d*" means any number of digit characters (including zero!)
"." means the literal '.' character (we need to escape it with a '\' first)
"\d{1,10}" means between 1 and 10 amount of digits
"\s" means any type of whitespace
I would recommend you take a look at https://regexone.com/. An excellent resource that will help you understand how regex works, it helped me a lot!

Try this:
/^[0-9]*[.]{0,1}[0-9]{1,10}$/
The Explanation follows:
^ asserts position at start of the string
[0-9]* Match a single character in the range between 0 (index 48) and 9 (index 57) any times
[.]{0,1} Match a single character . at max one times
[0-9]{1,10} Matches between 1 and 10 times, a single character in the range between 0 (index 48) and 9 (index 57)

Related

Regular Expression to validate a string containing '=' and '~' as end character [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
In Java, I have to validate a string which contains "~" and '=' at the end using RegEx.
For example:
LOCKER=2004-02-23-23.28.22.377655~UCC=0103207031~URY=31/12/9999~URF=23/02/2004~URT=SEREST ISSY LES MO ~URFC=XX~URFNUMCB=XXXXXXXXXXX~CEB=XXXXX~CEBC=XXXXX~URFN=0001
this String format is KEY1=VALUE~KEY2=VALUE~KEYN=VALUE uppercase
'~' is as separator
Currently, i am using some regular expresion but all of them false
can anyone help me please ? thank you for advanced
The following regex should do:
^(?!~)(?:(?:^|~)[^=~]+=[^=~]*)+$
Explanation
^ Match beginning-of-input, i.e. matching must start at beginning
(?!~) Input cannot start with `~`
(?: Repeat 1 or more times:
(?:^|~) Match beginning of input or match '~', i.e. match nothing on
first repetition, and match `~` on each subsequent repetition
[^=~]+ Match KEY
= Match '='
[^=~]* Match VALUE (may be blank)
)+
$ Match end-of-input, i.e. matching must cover all input
Change the characters classes for KEY and VALUE as needed if they have further restrictions, e.g. use [A-Z][A-Z0-9]* instead of [^=~]+ if the KEY has to be an uppercase-only identifier.
If using Java's matches() with the regex, the first ^ and the ending $ is redundant. The second ^ is still required.

What does regular expression (?<=[\\S])[\\S]*\\s* do? [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I saw a regex expression in this other stackoverflow question but I didn't understand the meaning of each part.
String[] split = s.split("(?<=[\\S])[\\S]*\\s*");
The result of this is the Acronym of a sentence.
In order to understand a chaining regex expression should I start reading it from left to right or viceversa? How can I identify (or limit) each part?
Thank you for your answers.
(?<=[\\S]) states that the match should be preceded by \\S, that is, anything except for a space.
[\\S]* states that the regex should match zero or more non-space characters
\\s* matches zero or more spaces.
In essence, the regex finds a non-space character, and matches all non-space characters in front of it, along with the spaces after them.
The regex matches ohandas<space><space> and aramchand<space> from Mohandas Karamchand G
Thus, after using these matches to split the string, you end up with {"M", "K", "G"}
Note the two spaces that the regex matches after Mohandas, because the \\s* part matches zero or more spaces
To clarify suspircius regular expression you may use the websites https://regexr.com/ or https://regex101.com/
Both mark parts with colors and explain what they do. But you have to replace the double backslashes by single backslashes.

Blank space in array with String splitter [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
My objective is to separate the numbers from the string, but in my array's first position i get a blank space. So i need help for that not to happen.
str1 = "Y=9x1+29x2";
String[] split2 = str2.split("[^-?.?0-9]+");
Blank space at the start is due to presence of non-digit character at the start of your input.
You can remove all non-digits at start before splitting:
String linha = "Y=9x1+29x2";
String[] split = linha.replaceFirst("[^-.\\d]+", "").split("[^-.\\d]+");
for (String tok: split2)
System.out.println(tok);
Output:
9
1
29
2
I think your question is rather vague, but after looking at it, I'm guessing that you want to extract the numbers out of the string, where a "number" has this format: an optional minus sign, followed by an optional decimal point, followed by one or more digits. I suspect you also want to include numbers that have digits followed by a decimal point followed by more digits.
I'm guessing this is what you want, because of the ? you put in your regex. The problem is that inside square brackets, ? doesn't mean "optional", and it doesn't mean "zero or one of something". It means a question mark. The regex [^-?.?0-9] means "match one character that is not a digit, a period, a hyphen, or a question mark". A pattern in square brackets always matches one character, and you tell it what characters are OK (or, if you begin with ^, what characters are not OK). This kind of "character set" pattern never matches a sequence of characters. It just looks at one character at a time. If you put + after the pattern, it still looks at one character at a time; it just does so repeatedly.
I think what you're trying to do is to take a pattern that represents a number, and then say "look for something that doesn't look like that pattern", and you tried to do it by using [^...]. That simply will not work.
In fact, split() is the wrong tool for this job. The purpose of split is to break up a string whose delimiters match a given pattern. Using it when the strings you want to keep in the array match a given pattern doesn't work very well, unless the pattern is extremely simple. I recommend that you create a Matcher and use the find() method in a loop. find() is set up so that it can find all matching substrings of a string if you call it repeatedly. This is what you want to accomplish, so it's the right tool.

Explain this regular expression in Java [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 8 years ago.
Improve this question
Regular expression in java:
'String'.replaceAll("([aeioucgjkqsxyzbfpvwdtmn1234567890])\\1+", "$1")
Can someone explain what the different characters do?
Explanation:
[aeioucgjkqsxyzbfpvwdtmn1234567890] Matches a single character in the list.
([aeioucgjkqsxyzbfpvwdtmn1234567890]) Capturing group around the char class would capture that single character.
\1+ \1 is a pointer to refer the chars inside the group index 1. In our case, a single character is captured so it refers to that single character. \1+ means one or more occurrences of the characters inside group index 1.
For Example:
aaaa
The above regex would capture the first character and check if the following one or more characters are same as the first character which was captured. If yes, then the whole duplicated chars are replaced by a single char(which was inside group index 1 ), that is aaaa was replaced by a single a
DEMO
All letters that are listed between brackets will be replaced by $1 if after them comes a \1, which is a literal backslash one. The plus sign (+) means 1 or more.
Any sequence of 1 or more of the characters inside the brackets [...] will be replaced with $1.
For instance, this will remove all those characters from your string:
System.out.println(Str.replaceAll("([aeioucgjkqsxyzbfpvwdtmn1234567890])\1+", ""));

Java matches regular expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Does anybody know what this means in java:
testNumber.matches("^800[25-9][2-9].*$")
I am mainly confused with [25-9].
[] is a subset. Any character in the brackets will match. 5-9 is a range of characters from 5 to 9. 2 is also a character. So [25-9] will match 2, 5, 6, 7, 8, and 9.
[25-9] means any character from 2 or 5-9
You can get explanation of your regex by yourself from here: http://rick.measham.id.au/paste/explain.pl
Here is a explaination about you Regular Expression:
/^800[25-9][2-9].*$/
^ assert position at start of the string
800 matches the characters 800 literally
[25-9] match a single character present in the list below
2 the literal character 2
5-9 a single character in the range between 5 and 9
[2-9] match a single character present in the list below
2-9 a single character in the range between 2 and 9
.* matches any character (except newline)
Quantifier: Between zero and unlimited times, as many times as
possible, giving back as needed [greedy]
$ assert position at end of the string
Based on http://regex101.com/ and https://www.debuggex.com/
^800[25-9][2-9].*$
^ assert position at start of the string
800 matches the characters 800 literally
[25-9] match a single character present in the list below
2 the literal character 2
5-9 a single character in the range between 5 and 9
[2-9] match a single character present in the list below
2-9 a single character in the range between 2 and 9
.* matches any character (except newline)
* Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string
In other words [...] is character class. It means it matches one of characters placed inside it like [123] means it matches either 1 2 3. You can also write this range as [1-3].
In case of [25-9] it means it will match 2 or one of digits in rage 5-9 which are 5, 6, 7, 8, 9.

Categories