Java regex: select from a set of characters [duplicate] - java

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
I am parsing text and need to detect if any string contains any character NOT including A-Z, 0-9, full stop, comma, plus, minus, or any number of spaces.
I tried the regex expression: "[^A-Z0-9][^.][^,][^-][^+][\S+]"
as well as variations on this, which does not work correctly.
Examples of permissible strings:
1 23842U 96021A 15170.20596865 .00000124 00000-0 00000+0 0 9998
2 23842 0.0589 306.1344 0002868 147.0577 292.5546 1.00269795 70198
Invalid string:
1 2%8!2U 96021A 15170.20596865 .00000124 ^00000-0 00000+0 0 9998

Seems like you want to allow, spaces, alphabets, digits, dot, plus, minus.
Pattern p = Pattern.compile("^[A-Za-z,.+\\s\\d-]+$");

Related

Java regex to match 10 digit phone number having spaces in between [duplicate]

This question already has answers here:
Ignoring white space for a Regex match
(3 answers)
How to ignore white space with regex?
(2 answers)
Closed 2 years ago.
I am trying to match
123 6523 8745
1234 65 3212
etc many combinations
The i want the regex to ignore the spaces and count 10 digits in a single line. For example [\d]{10} doesn't ignore the spaces, it will match any contiguous sequence of 10 digits. But here I want to ignore the spaces and consider these 10 digits as a contiguous sequence.
Thanks for suggestions.
Try this:
([\d] *){10}
(That's my whole answer but I have to write some more.)

Regular expression to match +-*/ any of these characters just once. In Java [duplicate]

This question already has answers here:
In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign?
(5 answers)
Closed 3 years ago.
I am trying to make a calculator in android with Java. Where I need to split the strings, to separate the digits and operands. Then, I will do the necessary operations with the digits. I need a regular expression to get it done.
I have tried this:
String[] numbers = screenText.split("[+\-/*]";
I am getting an error: Illegal character range.
The regular expression to split the operand is "[+\\-/*]" but you probably looking for something like https://en.wikipedia.org/wiki/LL_grammar

Regular expression to match a word starts and ends with a combination of special characters [duplicate]

This question already has answers here:
Java - Best way to grab ALL Strings between two Strings? (regex?)
(3 answers)
Closed 4 years ago.
I am trying to write a regular expression which gives me words which starts with <!= and ends with =>. For example if there is a sentence what is your <!=name=>, the result should give me name because it matches my pattern.
I have read to use this ^ for starts with and $ for ends with, but I am not able to match a combination of special characters.
As in the comment. You can use <!=(\w+)=> because the exclamation mark and equal sign are not part of word-character class you can simply test for those characters and match the word characters between them. check:https://regex101.com/r/qDrobh/4
For multiple words you can use:<!=((?:\w+| )*)=>
See:https://regex101.com/r/qDrobh/5

Advanced regex for unordered characters and numbers [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 5 years ago.
This seems simple but I've been struggling for hours now. The pattern is simple, a telephone number that optionally starts with a +, has 10-15 digits, and optionally has spaces, dashes or parentheses. The numbers and characters should be in no particular order.
I've tried using non-matching groups and seen so many different ways of validating phone numbers, but to no avail.
The best I have so far is ^\+?([0-9]{10-15}[)( -]*)$, but it only accepts the other characters if they're at the end of the pattern. This expression will be used in a Java context.
Regular Expression
\+?([\s-]?[0-9]){10,15}+
as a Java string
"\\+?([\\s-]?[0-9]){10,15}+"

Regex for arithmetic expression [duplicate]

This question already has answers here:
In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign?
(5 answers)
Closed 5 years ago.
The regex -?\d+ [+|-|*|/] -?\d+ matches expression 1 + 3 without any problems also 1 + -2 without any problems, but I don't know why it does not match 1 - 2. Could you explaing why it does not match the - char correctly?
By my regex I wanted to achieve:
optional - at the beginning
string of digits
whitespace then operator then whitespace
optional - before second stringof digits
A - unescaped in the middle of a character class creates a range. You can escape it or move it to the start or end of the character class. You also don't need/want the |s I'd guess.
You currently make a range between | and | which doesn't really make sense. You also could just use grouping instead of a character class.
(\+|-|\*|/)
With this approach the + and * need to be escaped because they are quantifiers when outside a character class.

Categories