This question already has answers here:
Is it possible to build a Pattern based on two sub patterns in Java
(2 answers)
Closed 3 years ago.
I am trying to make it so the user can enter 07XXXXXXXXX or 07XXX XXXXXX, both should work, but my RegEx seems to not be working correctly
I have tried adding \\d, \\s, \\w and none of that has worked
My code is:
//The space should be between {3} and [0-9]
while (!Scanner.hasNext("07[0-9]{9} | 07[0-9]{3} [0-9]{6}")) {
System.out.println("\nThe mobile number you have entered is not valid.\nIt should start with 07 and should contain 9 additional digits.");
Scanner.nextLine();
} //end while
The program should accept both 07XXXXXXXXX and 07XXX XXXXXX, but when i try and include the space, it doesn't accept either option.
"07[0-9]{9}" works for 07XXXXXXXXX on its own, but when I add "| 07[0-9]{3} [0-9]{6}", neither option works.
If you follow the space with a ? question mark it will mean "zero or one of" the space
07[0-9]{3} ?[0-9]{6}
You can also use number pairs in brackets to give a range:
07[0-9]{3} {0,1}[0-9]{6}
If you plan to allow other characters like hyphen you can make a character class for those. This allows space, hyphen or dot:
07[0-9]{3}[-. ]{0,1}[0-9]{6}
07[0-9]{3}[-. ]?[0-9]{6}
I recommend you put hyphen first in a character class as it can be taken to mean "range" like you have with 0-9. By putting it first it doesn't define a range. Also a period normally means "any character" but inside a character class it loses this special meaning and is literally just a period
Related
This question already has answers here:
How can I make Java print quotes, like "Hello"?
(11 answers)
What is the backslash character (\\)?
(6 answers)
Closed 2 years ago.
basically i want the output to be Path = "C:\Users\Public" and it seems to me that System.out.println("Path = "C:\Users\Public""); should work, but it doesn't so the question is why can't java just print the phrase as a combination of characters?
btw. this is my second time "programing" so please in simple terms if possible.
You should escape the special character such as " and \
System.out.println("Path = \"C:\\Users\\Public\"");
When you use backslash (\), java assumes that you are going to use an escape character. If you want to print that line you should probably use the method below and it should work. And you are using a string inside another one so you should use different types of inverted commas for both to tell the compiler that yeah there is a string inside another one. Otherwise the compiler will assume the middle inverted comma to be the closing one.
You can escape each and every escape character. If you notice I have used a backslash before inner inverted commas (" ") and every other backslash (\). Using a backslash before any escape character escapes it. So this should work.
System.out.println("Path = \"C:\\Users\\Public\"");
I hope now you can print your required output.
I am trying to write a regex for the following format
PA-123456-067_TY
It's always PA, followed by a dash, 6 digits, another dash, then 3 digits, and ends with _TY
Apparently, when I write this regex to match the above format it shows the output correctly
^[^[PA]-]+-(([^-]+)-([^_]+))_([^.]+)
with all the Negation symbols ^
This does not work if I write the regex in the below format without negation symbols
[[PA]-]+-(([-]+)-([_]+))_([.]+)
Can someone explain to me why is this so?
The negation symbol means that the character cannot be anything within the specified class. Your regex is much more complicated than it needs to be and is therefore obfuscating what you really want.
You probably want something like this:
^PA-(\d+)-(\d+)_TY$
... which matches anything that starts with PA-, then includes two groups of numbers separated by a dash, then an underscore and the letters TY. If you want everything after the PA to be what you capture, but separated into the three groups, then it's a little more abstract:
^PA-(.+)-(.+)_(.+)$
This matches:
PA-
a capture group of any characters
a dash
another capture group of any characters
an underscore
all the remaining characters until end-of-line
Character classes [...] are saying match any single character in the list, so your first capture group (([^-]+)-([^_]+)) is looking for anything that isn't a dash any number of times followed by a dash (which is fine) followed by anything that isn't an underscore (again fine). Having the extra set of parentheses around that creates another capture group (probably group 1 as it's the first parentheses reached by the regex engine)... that part is OK but probably makes interpreting the answer less intuitive in this case.
In the re-write however, your first capture group (([-]+)-([_]+)) matches [-]+, which means "one or more dashes" followed by a dash, followed by any number of underscores followed by an underscore. Since your input does not have a dash immediately following PA-, the entire regex fails to find anything.
Putting the PA inside embedded character classes is also making things complicated. The first part of your first one is looking for, well, I'm not actually sure how [^[PA]-]+ is interpreted in practice but I suspect it's something like "not either a P or an A or a dash any number of times". The second one is looking for the opposite, I think. But you don't want any of that, you just want to start without anything other than the actual sequence of characters you care about, which is just PA-.
Update: As per the clarifications in the comments on the original question, knowing you want fixed-size groups of digits, it would look like this:
^PA-(\d{6})-(\d{3})_TY$
That captures PA-, then a 6-digit number, then a dash, then a 3-digit number, then _TY. The six digit number and 3 digit numbers will be in capture groups 1 and 2, respectively.
If the sizes of those numbers could ever change, then replace {x} with + to just capture numbers regardless of max length.
according to your comment this would be appropriate PA-\d{6}-\d{3}_TY
EDIT: if you want to match a line use it with anchors: ^PA-\d{6}-\d{3}_TY$
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I want to replace &sp; in the string below with Z.
Input text : ABCD&sp;EF&p;GHIJ&bsp;KL
Output text : ABCDZEFZGHIZKL
Can anyone tell me how to replace the every instance of &\D+; using java regular expression?
I am using /(&\D+;)?/ but it doesn't work.
Use String#replaceAll.
You also should use the ? modificator to +:
String str = "ABCD&sp;EF&p;GHIJ&bsp;KL";
String regex = "&\\D+?;";
System.out.println (str.replaceAll(regex,"Z"));
This should work
Match the initial &, then all characters that are not the tailing ;, then that tailing ; like so: &[^;]+; If not matching numbers (as suggested by your example with \D) is a requirement, add the numbers to the negated character set: [^;0-9] To make it replace all occurrences, add the global flag g. The site regexr.com is a handy tool to create regexes.
Edit: Sorry, I initially read your question wrong.
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
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-]+$");