Matching Multiple Patterns

Matching multiple regex patterns with the alternation operator?

From the documentation of re.findall:

If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.

While your regexp is matching the string three times, the (.*?) group is empty for the second two matches. If you want the output of the other half of the regexp, you can add a second group:

>>> re.findall(r'\((.*?)\)|(\w)', '(zyx)bc')
[('zyx', ''), ('', 'b'), ('', 'c')]

Alternatively, you could remove all the groups to get a simple list of strings again:

>>> re.findall(r'\(.*?\)|\w', '(zyx)bc')
['(zyx)', 'b', 'c']

You would need to manually remove the parentheses though.

Regex to match multiple patterns

Use ? for optional pattern

I assume the difference between the [] boxes is the searched string must start with a capital letter, and the part from ! onwards is optional. You can make something optional with a ? behind it.

You can use this regular expression to find your example texts:

\[([A-Z][A-Za-z0-9-]+(![\d.]+)?)\]

See here to play around with it:

https://regex101.com/r/5ekTkJ/1

Match strings with multiple regex patterns in javascript

Using this line in the loop if(!match.index || !regexObj.lastIndex) break; will stop the loop when either of the statements in the if clause are true.

If either the match.index or regexObj.lastIndex is zero, this will be true and the loop will stop, and this will happen for example if there is a match for the first character as the index will be 0.

You can also switch the order of the patterns, putting the most specific one first. Because the first char of the email will also be matched by [a-z] so the email will otherwise not be matched.

Note to omit the anchors ^ and $ from the email or else the email will only match if it is the only string.

let str = "I am abinas patra and my email is abinas@gmail.com"
let patterns = [
"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}",
"[a-z]"
];
let regexObj = new RegExp(patterns.join("|"), "gmi");
let match, indicesArr = [];
while ((match = regexObj.exec(str))) {
let obj = {
start: match.index,
end: regexObj.lastIndex
}
indicesArr.push(obj);
}
console.log(indicesArr)

how to match multiple patterns in string?

grep category of table2 with names of table1 and get the values of names of table1 and assign it to table2. Once we have names in both tables, we can use join approach based on = .(names) and bind the size from table2 to table1.

  library(data.table)      
table_2 <- table_2[, .(names = grep( unique(category), table_1[, names], value = TRUE ), size = size ),
by = category ]
table_2 <- table_2[!is.na(names), ]

table_1[table_2, `:=` ( size = i.size), on = c('names')]
table_1
# names value size
# 1: bluecdsd 0.2655087 little
# 2: red321 0.3721239 large
# 3: yellowVsds523 0.5728534 NA
# 4: 423_black 0.9082078 small
# 5: ewrwblack 0.2016819 small

Data:

set.seed(1)
table_1 <- data.table(names = c('bluecdsd','red321','yellowVsds523','423_black','ewrwblack'),
value = runif(5))

table_2 <- data.table(category = c('black','blue','red','white'),
size = c('small','little','large','huge'))

How to match multiple strings with Where-Object/Wildcards?

You started off correctly, however, -like lets you match only wildcard patterns whereas -match let you match regex patterns and your regex just needs a little tweaking:

Get-Command | ?{$_ -match "((^set)|(^get)|(^convertto))-[CXV]+"}

This can be further shortened to:

Get-Command | ?{$_ -match "((^[sg]et)|(^convertto))-[CXV]+"}

If you want a sorted output of the commands:

Get-Command | ?{$_ -match "((^[sg]et)|(^convertto))-[CXV]+"} | Sort

Ref: About Comparison Operators



Related Topics



Leave a reply



Submit