This question already has answers here:
Regular Expressions: How to Express \w Without Underscore
(7 answers)
Closed 2 years ago.
Currently have this regex string in my java code:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It successfully accepts those characters, however it also accepts underscore, how can modify the regex to reject underscore appearing anywhere in the string?
Your regex is:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It is using \w which is equivalent of [a-zA-Z0-9_], hence it allows underscore also.
You can change your character class to this:
^[-#. a-zA-Z0-9\\/]{0,70}$
Note that space, dot, #, / don't need to be escaped inside [...] and - if placed at first or last position doesn't require escaping either.
Related
This question already has answers here:
Regular expression for first and last name
(28 answers)
Closed 2 years ago.
I'm looking for a solution in regex. I have a full name for which the allowed characters are a-z, A-Z, space and /,-.#'
Also it should not start with a blank character/space.
So basically the following names are accepted.
Anjith Sasindran
Anjith# Sasindran'
Anjith
Anjith/,Sasi#n'ran
An-. Sasindr#
And the following are not
An%jith Sasindran
Anjit*) Sasindran
Basically anything other than the ones I listed above.
I'm not sure how to do the same. I've very little knowledge in regex b/w. So any help would be appreciated.
use:
^[^\s][ A-Za-z-'#.,/]*$
^[^\s] checks that there is no whitespace at the beginning
A-Za-z accepts all the alphabets in the given string
-'#.,/ accepts only these special characters
* matches 0 or more preceding token
With regular expressions, you can define classes of characters like the follwing:
[\sa-zA-Z#\.,\-'/].
With that, you define one "allowed" character. You can specify a sequence of it by adding plus operator : [\sa-zA-Z#\.,\-'/]+.
Now, to avoid texts starting with blank character, you can create a character class with all wanted characters except spaces [a-zA-Z#\.,\-'/].
So, now, the final regex is "any of the wanted characters except a blankspace, followed by any of wanted characters a certain number of times" : [a-zA-Z#\.,\-'/][\sa-zA-Z#\.,\-'/]*
With java, you must use Pattern class to apply regex checks on a string:
var nameMatcher = Pattern.compile("[a-zA-Z#\\.,\\-'/][\\sa-zA-Z#\\.,\\-'/]*").asPredicate();
if (nameMatcher.test("Anjhit#")) System.out.println("Name match !");
You can get a lot of information with Oracle documentation.
This question already has answers here:
Trim leading or trailing characters from a string?
(6 answers)
Closed 2 years ago.
I'd like to trim any single trailing . or -. I tried doing this by doing something like "f-o.o.".replaceFirst("^(\\.+)[-|.]$", "$0"). The expected string is f-o.o but I'm getting f-o.o.. Thank you.
Your expression has two mistakes:
you put a slash in front of a dot, making it match a literal dot, not just any character
you put | into a character class, so your expression would remove not just . or - at the end of the string, but also |.
Use "f-o.o.".replaceFirst("[-.]$", "")
This question already has answers here:
How do I use a delimiter with Scanner.useDelimiter in Java?
(3 answers)
Closed 5 years ago.
I want the scanner to ignore three things: empty spaces, "/" and "!".
What is the correct argument to use in the useDelimiter method?
useDelimiter takes a regex argument docs:
pattern - A string specifying a delimiting pattern
So just make sure the string is in regex form.
Whitespace in regex is \s, escape that to become \\s. / is still / and ! is still !. You then use | to act as an "or" operator to say "either one of these".
Here's how to do it:
scanner.useDelimiter("\\s|/|!");
If you want to say that "consecutive whitespaces slashes and exclamation marks also count as delimiter", then you can add a quantifier + to the whole thing:
scanner.useDelimiter("(\\s|/|!)+");
Scanner's delimiter is just a pattern, so you could use the following:
sc.useDelimiter("[\\s/!]*");
This question already has answers here:
Regex match entire words only
(7 answers)
Closed 5 years ago.
I have a java regex for replacing all instances of a specific identifier in a script.
This is the search regex that searchers for the "foo" identifier:
([^\w_]|^)foo([^\w\d_]|$)
And this is the replacement:
$1bar$2
Doing a replaceAll in something like
for foo: [1,2,3];foo&&foo;
works well, it outputs
for bar: [1,2,3];bar&&bar;
However, when we apply this to a string with two instances of the identifier separated by a single character, it only replaces the first:
foo&foo
outputs
bar&foo
This happens, I think, because the first match is "bar&" and so when analyzing the rest of the string no other match is found.
Is there a way to fix this by changing the regex only?
I think you are almost looking for \bfoo\b as your regex otherwise use lookarounds (?<=\W|^)foo(?=\W|$). In both ways replacement string is bar.
Note: \d and _ are subsets of \w and [^\w] is equal to \W
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 6 years ago.
I am trying to solve following task:
Match the pattern abc.def.ghi.jkl, where each variable a,b,c,d,e,f,g,h,i,j,k,l can be any single character except the newline.
For above question I am matching the input to regex :
"([^\\n]{3}(.)){3}([^\\n]{3})"
// this is the regex pattern I am using currently
What am I doing wrong? Please help me correct the above regex so that it does not match the incorrect input I have provided in the title. Currently it matches to it somehow. Although I have provided 3 it is apparently matching to more than 3 characters.
. has a special meaning in regular expression patterns.
If you want to get a "simple dot", you need to quote/escape it (as "\\.").
And that special meaning is (under normal configuration) "any character except line breaks", which exactly matches your other condition, so you can simplify this to
"(...)\\.(...)\\.(...)\\.(...)"