Regex pattern match string irrespective of order [closed] - regex

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a comma-separated string and wanted to check if the string has all words irrespective of order using REGEX pattern.
Example: Original String = "help, assist, aid";
Use Cases:
"help, assist, aid" -> return true
"aid, assist, help" -> return true
"aid, help,assist" -> return true
"aid, help" -> return false
"aid, help,support" -> return false
Can someone help me in writing the regex pattern for this use case?

You could use three positive lookahead assertions:
^(?=.*\bhelp\b)(?=.*\baid\b)(?=.*\bassist\b).*$
Each of the three lookaheads is actually zero width, and so would all be evaluated once at the very start of the pattern.
Demo

Related

I wanted to remove particular pattern from a string in tcl [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
hreset_error_status_t2_reg1
htrans_flag_t1_reg2
hsize_flag_error_reg
check_hwdata_reg
I had to write a Tcl script using regex I have to remove error_status, flag, flag_error, and check in the following strings. It has to search the following keywords in the file and have to remove the mentioned keywords in a string.
To remove all those patterns from a string, use regsub -all. If you don't need to use the power of regular expressions, then string map works well too.
set my_str "hreset_error_status_t2_reg1 htrans_flag_t1_reg2 hsize_flag_error_reg check_hwdata_reg"
set my_regex {error_status_|flag_(error_)?|check_}
regsub -all $my_regex $my_str ""
--> hreset_t2_reg1 htrans_t1_reg2 hsize_reg hwdata_reg
or
set my_map {
error_status_ ""
flag_error_ ""
flag_ ""
check_ ""
}
string map $my_map $my_str
--> hreset_t2_reg1 htrans_t1_reg2 hsize_reg hwdata_reg
Note that I put flag_error_ before flag_ in $my_map so that only the flag_ characters in flag_error weren't removed.

Regex Match in between Strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I using Regex to extract a pattern which is of the form [a-zA-z][0-9]{8} Ex:K12345678
I need to extract this pattern from a string and this pattern should be matched properly
I tried the below but my testcase if failing for this scenario
This is my Regex /[a-zA-Z][0-9]{8}/g
phonne number:9978434276K12345678:My pattern
For this scenario it is failing.
My Sample Code
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);
The expected output is K12345678.The Regex which I wrote does not handle this.
You can use String.match(Regex)
"9978434276K12345678".match(/[a-zA-Z][0-9]{8}/)
It returns an array of 4 elements: [String coincidence, index: Number, input: String, groups: undefined]
just stay with the element 0: coincidence and 1: index of the match.
and use this just to check that the string matches at least one
/Regex/.test(String)
/[a-zA-Z][0-9]{8}/.test("9978434276K12345678")
It will return true or false
USE expression without quotation marks
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);

Split a String by Regex Expression - Split the whole String by the Character '=' only if the Character on the left is not # in Kotlin [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
e.g.
If i have a String like val input = "2+2#=4=test2" should return a List
2+2#=4
test2
order is not important.
You may try splitting using a lookbehind which asserts that what precedes the = sign is not a hash symbol:
val input = "2+2#=4=test2";
val arr = input.split("(?<!#)=".toRegex());
println(arr);
This prints:
[2+2#=4, test2]

Better way to find if value matches list of regular expressions [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Sample code, see if a value matches any regex from a list:
private val regex: List<Regex> = ...
fun matches(value: String): Boolean {
regex.forEach { re ->
if (re.matches(value))
return true
}
return false
}
It works, but I'm wondering if there is a more elegant way to do this. Note that I don't want to evaluate all of them, but stop whenever there is a match.
fun matches(value: String) = regex.any { it.matches(value) }
any
There are also similar functions:
all,
none

Check a string has an integer in the last part using Regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have few lines
Eg:
word1-word2-word3-1
word1-word2-word3-23
word1-word2-word3-dr
word1-word2-word3-13-4
word1-word2-word3-drg
word1-word2-word3-word4-4
From the above lines i need the following lines by using Regex
word1-word2-word3-1
word1-word2-word3-23
word1-word2-word3-word4-4
ie. The string ending with a "-{int}"
You can use the pattern -\d+$
The pattern means that we are look for;
-\d+ - hyphen and one or more digits
$ Asserts that we are at the line end.
I don't know what language you are writing this in but assuming you are doing it in Javascript. This pattern can be proved using the code below.
"use strict";
var values = [
"word1-word2-word3-1",
"word1-word2-word3-23",
"word1-word2-word3-dr",
"word1-word2-word3-13-4",
"word1-word2-word3-drg",
"word1-word2-word3-word4-4"
];
values.forEach(value => {
if (/.*-(?!\d+-)\w+-\d+$/.exec(value)) {
console.log(value + " has - integer at the end!");
};
});