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
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:
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.
This question already has answers here:
Using Regular Expressions to Extract a Value in Java
(13 answers)
Closed 3 years ago.
This is intended to be used in Java.
Imagine following sample input:
WRA1007
1085808
1092650S
3901823CV
I want to match all alphabetic characters after at least one digit.
Desired output:
S
CV
Actual output:
0S
3CV
My current approach looks like this:
\d[a-zA-Z]+
The problem with this pattern is that it includes the digit beforehand too. My current solution is to remove the first character of the resulting string afterwards. And this seems quite unsatisfactory to me.
You need a lookbehind:
(?<=\d)[a-zA-Z]+
(?<=\d) means "there must be a digit before this position, but don't match it".
Demo
Alternatively, you can use a pair of () to surround the part you want to get:
\d([a-zA-Z]+)
This is called a "group", and you can get its value by calling group(1) on your Matcher.
If you 'add' groups you can get group 1 that contain only letters
\d([a-zA-Z]+)
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
"(...)\\.(...)\\.(...)\\.(...)"
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to escape text for regular expression in Java
Is there a built in way or a standard library for cleaning arbitrary strings for use in regex?
As in, if I have the string something .* foo and I want to turn that into a regex like ^something \.\* foo$ is that something that can be easily done?
You can use Pattern.quote(String) for this purpose. From the docs:
Returns a literal pattern String for the specified String.
This method produces a String that can be used to create a Pattern that would
match the string s as if it were a literal pattern.
Metacharacters or escape sequences in the input sequence will be given
no special meaning.