How to match a string in this way?
up vote
0
down vote
favorite
I need to check if a String matches this specific pattern.
The pattern is:
(Numbers)(all characters allowed)(numbers)
and the numbers may have a comma ("." or ",")!
For instance the input could be 500+400 or 400,021+213.443.
I tried Pattern.matches("[0-9],?.?+[0-9],?.?+", theequation2), but it didn't work!
I know that I have to use the method Pattern.match(regex, String), but I am not being able to find the correct regex.
java regex
add a comment |
up vote
0
down vote
favorite
I need to check if a String matches this specific pattern.
The pattern is:
(Numbers)(all characters allowed)(numbers)
and the numbers may have a comma ("." or ",")!
For instance the input could be 500+400 or 400,021+213.443.
I tried Pattern.matches("[0-9],?.?+[0-9],?.?+", theequation2), but it didn't work!
I know that I have to use the method Pattern.match(regex, String), but I am not being able to find the correct regex.
java regex
2
please, give possible input and output, and show your attempt.
– The Scientific Method
Nov 10 at 18:21
Done! I have added them"
– AyhamSYR
Nov 10 at 18:26
If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
– KevinO
Nov 10 at 18:39
Could you give an example please?
– AyhamSYR
Nov 10 at 18:41
Your requirement is too vague. You might as well uses.matches("\d.*\d")to match a string starting and ending with a digit and having any 0+ chars in between.
– Wiktor Stribiżew
Nov 10 at 20:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to check if a String matches this specific pattern.
The pattern is:
(Numbers)(all characters allowed)(numbers)
and the numbers may have a comma ("." or ",")!
For instance the input could be 500+400 or 400,021+213.443.
I tried Pattern.matches("[0-9],?.?+[0-9],?.?+", theequation2), but it didn't work!
I know that I have to use the method Pattern.match(regex, String), but I am not being able to find the correct regex.
java regex
I need to check if a String matches this specific pattern.
The pattern is:
(Numbers)(all characters allowed)(numbers)
and the numbers may have a comma ("." or ",")!
For instance the input could be 500+400 or 400,021+213.443.
I tried Pattern.matches("[0-9],?.?+[0-9],?.?+", theequation2), but it didn't work!
I know that I have to use the method Pattern.match(regex, String), but I am not being able to find the correct regex.
java regex
java regex
edited Nov 10 at 20:11
Wiktor Stribiżew
305k16123200
305k16123200
asked Nov 10 at 18:19
AyhamSYR
72
72
2
please, give possible input and output, and show your attempt.
– The Scientific Method
Nov 10 at 18:21
Done! I have added them"
– AyhamSYR
Nov 10 at 18:26
If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
– KevinO
Nov 10 at 18:39
Could you give an example please?
– AyhamSYR
Nov 10 at 18:41
Your requirement is too vague. You might as well uses.matches("\d.*\d")to match a string starting and ending with a digit and having any 0+ chars in between.
– Wiktor Stribiżew
Nov 10 at 20:13
add a comment |
2
please, give possible input and output, and show your attempt.
– The Scientific Method
Nov 10 at 18:21
Done! I have added them"
– AyhamSYR
Nov 10 at 18:26
If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
– KevinO
Nov 10 at 18:39
Could you give an example please?
– AyhamSYR
Nov 10 at 18:41
Your requirement is too vague. You might as well uses.matches("\d.*\d")to match a string starting and ending with a digit and having any 0+ chars in between.
– Wiktor Stribiżew
Nov 10 at 20:13
2
2
please, give possible input and output, and show your attempt.
– The Scientific Method
Nov 10 at 18:21
please, give possible input and output, and show your attempt.
– The Scientific Method
Nov 10 at 18:21
Done! I have added them"
– AyhamSYR
Nov 10 at 18:26
Done! I have added them"
– AyhamSYR
Nov 10 at 18:26
If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
– KevinO
Nov 10 at 18:39
If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
– KevinO
Nov 10 at 18:39
Could you give an example please?
– AyhamSYR
Nov 10 at 18:41
Could you give an example please?
– AyhamSYR
Nov 10 at 18:41
Your requirement is too vague. You might as well use
s.matches("\d.*\d") to match a string starting and ending with a digit and having any 0+ chars in between.– Wiktor Stribiżew
Nov 10 at 20:13
Your requirement is too vague. You might as well use
s.matches("\d.*\d") to match a string starting and ending with a digit and having any 0+ chars in between.– Wiktor Stribiżew
Nov 10 at 20:13
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.
This Java regex handles the requirements:
"((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"
However, there is a potential issue in the above. Consider the following:
300 - -200. The foregoing won't match that case.
Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:
"((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"
Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.
You can see this regular expression here
add a comment |
up vote
0
down vote
In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+
For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.
Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.
d+(?:[.,]d+)?.d+(?:[.,]d+)?
In Java:
String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";
Regex demo
That would match:
d+(?:[.,]d+)?1+ digits followed by an optional part that matches.or,followed by 1+ digits
.Match any character (Use .+) to repeat 1+ times- Same as the first pattern
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.
This Java regex handles the requirements:
"((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"
However, there is a potential issue in the above. Consider the following:
300 - -200. The foregoing won't match that case.
Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:
"((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"
Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.
You can see this regular expression here
add a comment |
up vote
1
down vote
accepted
Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.
This Java regex handles the requirements:
"((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"
However, there is a potential issue in the above. Consider the following:
300 - -200. The foregoing won't match that case.
Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:
"((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"
Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.
You can see this regular expression here
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.
This Java regex handles the requirements:
"((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"
However, there is a potential issue in the above. Consider the following:
300 - -200. The foregoing won't match that case.
Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:
"((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"
Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.
You can see this regular expression here
Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.
This Java regex handles the requirements:
"((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"
However, there is a potential issue in the above. Consider the following:
300 - -200. The foregoing won't match that case.
Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:
"((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"
Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.
You can see this regular expression here
edited Nov 10 at 19:00
answered Nov 10 at 18:42
KevinO
3,04131628
3,04131628
add a comment |
add a comment |
up vote
0
down vote
In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+
For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.
Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.
d+(?:[.,]d+)?.d+(?:[.,]d+)?
In Java:
String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";
Regex demo
That would match:
d+(?:[.,]d+)?1+ digits followed by an optional part that matches.or,followed by 1+ digits
.Match any character (Use .+) to repeat 1+ times- Same as the first pattern
add a comment |
up vote
0
down vote
In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+
For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.
Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.
d+(?:[.,]d+)?.d+(?:[.,]d+)?
In Java:
String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";
Regex demo
That would match:
d+(?:[.,]d+)?1+ digits followed by an optional part that matches.or,followed by 1+ digits
.Match any character (Use .+) to repeat 1+ times- Same as the first pattern
add a comment |
up vote
0
down vote
up vote
0
down vote
In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+
For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.
Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.
d+(?:[.,]d+)?.d+(?:[.,]d+)?
In Java:
String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";
Regex demo
That would match:
d+(?:[.,]d+)?1+ digits followed by an optional part that matches.or,followed by 1+ digits
.Match any character (Use .+) to repeat 1+ times- Same as the first pattern
In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+
For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.
Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.
d+(?:[.,]d+)?.d+(?:[.,]d+)?
In Java:
String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";
Regex demo
That would match:
d+(?:[.,]d+)?1+ digits followed by an optional part that matches.or,followed by 1+ digits
.Match any character (Use .+) to repeat 1+ times- Same as the first pattern
edited Nov 11 at 13:35
answered Nov 11 at 12:22
The fourth bird
19.4k71323
19.4k71323
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242034%2fhow-to-match-a-string-in-this-way%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
please, give possible input and output, and show your attempt.
– The Scientific Method
Nov 10 at 18:21
Done! I have added them"
– AyhamSYR
Nov 10 at 18:26
If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
– KevinO
Nov 10 at 18:39
Could you give an example please?
– AyhamSYR
Nov 10 at 18:41
Your requirement is too vague. You might as well use
s.matches("\d.*\d")to match a string starting and ending with a digit and having any 0+ chars in between.– Wiktor Stribiżew
Nov 10 at 20:13