Regex match exact string pattern not working [duplicate] - regex

This question already has answers here:
Regular expression pipe confusion
(5 answers)
Closed 20 days ago.
I am sure this question has been answered before, but I cannot find exactly what I was looking for on the Stackoverflow. Would you be kind enough to help me with my issue?
What is the issue? My regex pattern would stop on string #2 the minute is found the same pattern as string #1. In other words, it does not know that "deposit.accountNumberXXX" is not the same as "deposit.accountNumber".
How do I create a pattern that it will be able to make a distinction between string #1 and string #2?
I have two strings.
deposit.accountNumber
deposit.accountNumberXXXX
const findReplace = (valuesToBeReplaced: string, dictionaryKeyValue: { [x: string]: string }) => {
const keyValueString = Object.keys(dictionaryKeyValue).join('|');
const pattern = `${keyValueString}\\b`
const result = new RegExp(pattern, 'g');
// iterate through the Keys of the dictionary and return a replacement value
return valuesToBeReplaced.replace(result, (matched) => dictionaryKeyValue[matched]);
};
I have tried different patterns and could not get it to work.

You need parentheses around ${keyValueString}. Otherwise, the regexp looks like
word1|word2|word3\b
and the
\b word boundary is only applied to the last word.
const pattern = `(?:${keyValueString})\\b`

Related

How to find words with a specific letter using RegExp? [duplicate]

This question already has answers here:
How to find words containing a certain letter with Regular Expressions?
(2 answers)
Closed 1 year ago.
void main() {
var string = 'This is a statement with i.';
var regExp = RegExp('i');
for (var match in regExp.allMatches(string)) {
print(match.group(0));
}
}
I want to print all the words which has i in it by only using RegExp. For instance, the above for loop should print
This
is
with
i
Would checking for a word boundary work for you?
The pattern \b\w*i\w*\b with the global flag set matches:
This
is
with
i
Try it out!
I don't have access to my GitHub (to share a gist) at the moment, but this pattern works as expected in DartPad.

Regex express begin & end specific words[No duplicate] [duplicate]

This question already has answers here:
Regex matching beginning AND end strings
(6 answers)
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I'm trying to write a regex represent 'recognizes words that begin and end in "t".'
I think that the below code is true.
var re = /^t+t*t$/
But it shows 'false'
e.g.
re.test('triplet')
re.test('thought')
re.test('that')
why doesn't my answer solve the above string?
and what is the proper regex?
Your regex is wrong, as pointed out in the comments.
A naive approach could be to check if the entire word starts with t, has any number of any character and then ends with t:
var re = /^t.*t$/
of course, you could also limit the "middle" character to letters:
var re = /^t[a-z]*t$/
However, neither of these approaches check for a word that is a single "t" character. If this is a valid usecase, you'll have to handle it explicitly, e.g.:
var re = /^(t[a-z]*t|t)$/

Regexp for string stating with a + and having numbers only [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 4 years ago.
I have the following regex for a string which starts by a + and having numbers only:
PatternArticleNumber = $"^(\\+)[0-9]*";
However this allows strings like :
+454545454+4545454
This should not be allowed. Only the 1st character should be a +, others numbers only.
Any idea what may be wrong with my regex?
You can probably workaround this problem by just adding an ending anchor to your regex, i.e. use this:
PatternArticleNumber = $"^(\\+)[0-9]*$";
Demo
The problem with your current pattern is that the ending is open. So, the string +454545454+4545454 might appear to be a match. In fact, that entire string is not a match, but the engine might match the first portion, before the second +, and report a match.

Regex to match all character groups in a string [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I need a regex to match the groups of characters in a string.
For example this is-a#beautiful^day.
Should result in the following list: this, is, a, beautiful, day.
As a mention I don't know how long the string is or by what characters the words are separated.
Any ideas? I have no clue how to build a regex for this.
If you want find all groups of letters:
import re
string = "this is-a#beautiful^day"
list = re.findall(r'[A-Za-z]+', string)
print list
['this', 'is', 'a', 'beautiful', 'day']

RegEx for matching characters unless they are contained in certain string [duplicate]

This question already has answers here:
Regex Pattern to Match, Excluding when... / Except between
(6 answers)
Closed 7 years ago.
Let's say I wanna match the letters E, Q and W. However, I don't want them matched if they're found in a certain string of characters, for instance, HELLO.
LKNSDG8E94GO98SGIOWNGOUH09PIHELLOBN0D98HREINBMUE
^ ^ ^ ^ ^
yes yes NO yes yes
There's a nifty regex trick you can use for this. Here's some code in JavaScript, but it can be adapted to any language:
var str = 'LKNSDG8E94GO98SGIOWNGOUH09PIHELLOBN0D98HREINBMUE',
rgx = /HELLO|([EQW])/g,
match;
while ((match = rgx.exec(str)) != null) {
if (match[1]) output(match[1] + '\n');
}
function output(x) { document.getElementById('out').textContent += x; }
<pre id='out'></pre>
Basically, you match on HELLO|([EQW]). Since regex is inherently greedly, if it comes across a HELLO, it'll immediately match that, thereby skipping the E inside of it.
Then you can just check the capture group. If there's something in that capture group, we know it's something we want. Otherwise, it must have been part of the HELLO, so we ignore it.