Advanced regex for unordered characters and numbers [duplicate] - java

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}+"

Related

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

Java regex not respecting the specified quantifier [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I have the following line from a CSV file:
Name,Age,Country,State,Zip,Phone,Email,Address
I am using the following Java regex to capture Name,Age,Country into 1 group but it always captures this:
Regex --> ^((?:.*,){3})
Result --> Name,Age,Country,State,Zip,Phone,Email,
Why is it not respecting the {3} quantifier I am using?
A dot matches a comma too. You have two solutions:
the bad one, make it not greedy: ^((?:.*?,){3})
the right one: exclude commas: ^((?:[^,]*,){3})
The first one is bad because it's expensive and has potential for catastrophic backtracking.

Stripping specific chars from beginning/ending of a string [duplicate]

This question already has answers here:
Regex to trim hyphens from start and end of a string
(2 answers)
Closed 7 years ago.
Given a word-string in Java, I want to strip off from beginning and from end, exactly these specified set of characters:
[?:!.,;'\"«»]
as many times as they appear.
For instance, «Be!!» should become just Be, "Here!!!" should become Here, «I should become I.
Can anyone provide a correct way to do this?
Use an anchored regex in string.replaceAll function.
string.replaceAll("^[?:!.,;'\"«»]+|[?:!.,;'\"«»]+$", "");
DEMO

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

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-]+$");

Categories