I'm looking for a regex to match this: a_*_*#example.com where the * is any text of any length. Doing this in NodeJS
Additionally I'm looking for a regex that matches any string not including the # symbol.
a_.*_.*#example\.com for the first
^[^#]*$ for the second
Related
I'm trying to match all these with just one regex:
https://m.tiktok.com/h5/share/usr/6641141594707361797.html
https://m.tiktok.com/v/6749869095467945218.html
https://www.tiktok.com/embed/6567659045795758085
https://www.tiktok.com/share/user/6567659045795758085
https://www.tiktok.com/trending?shareId=6744531482393545985
https://www.tiktok.com/#burntpizza89/video/7067695578729221378?is_copy_url=1&is_from_webapp=v1
https://www.tiktok.com/#burntpizza89/video/is_copy_url=1&is_from_webapp=v1&item_id=7067695578729221378
https://vm.tiktok.com/ZMF6rgvXY/
And it works fine except for the last one. The current regex is:
"\bhttps?:\/\/(?:m|www|vm)\.tiktok\.com\/.*\b(?:(?:usr|v|embed|user|video)\/|\?shareId=|\&item_id=)(\d+)\b"gm
It's handling all these digits ids perfectly (.tiktok.com/#burntpizza89/video/7067695578729221378), but I also need to match somehow these types of links which contains some specific url (.tiktok.com/ZMF6rgvXY/) with just one regex. So for the match I would get or digit-only id, or the url which contains digits and characters.
try the last part:
~https?://(?:www\.)?tiktok\.com/\S*/video/(\d+)|https?://(?:www\.)?vm.tiktok.com/\S*/~
I have an html text. With my regex:
r'(http[\S]?://[\S]+/favicon\.ico[\S^,]+)"'
and with re.findall(), I get this result from it:
['https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico?v=ec617d715196', 'https://stackoverflow.com/favicon.ico,https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico?v=ec617d715196']
But i dont want this second result in list, i understand that it has coma inside, but i have no idea how to exclude coma from my regex. I use re.findall() in order to find necessery link in any place in html text because i dont know where it could be.
Note that [\S]+ contains redundant character class, it is the same as \S+. In http[\S]?://, [\S]? is most likely a human error, as [\S]? matches any optional non-whitespace char. I doubt you implied to match http§:// protocol. Just use s to match s, or S to match S.
You can use
https?://[^\s",]*/favicon\.ico[^",]+
See the regex demo.
Details:
https?:// - http:// or https://
[^\s",]* - zero or more chars other than whitespace, " and , chars
/favicon\.ico - a fixed /favicon.ico string
[^",]+ - one or more chars other than a " and , chars.
in node js when i try to check for validation of incoming string using express-validator it doesn't match using
check('firstName').matches('^[a-zA-Z\s\'\-$]')
to parse firstName of incoming request body
Note I've edited the question to be like
check('firstName').matches('^[a-zA-Z\s\'\-]$')
I see two issues here:
The range issue because of \-. You should use double escaping character instead.
The given regex will only match the first character because the quantifier is missing. You should use the + (one or more characters) quantifier at the end of the regex for full match.
The correct regex for your case would be:
check('firstName').matches('^[a-zA-Z\s\'\\-$]+')
express was treating the string in different way than /regex/ this was the issue.
I have this for example:
<#445288012218368010>
And I want to get from between <# > symbols the value.
I tried so:
string.replace(/^(?:\<\#)(?:.*)(?:\>)$/gim, '');
But then I don't get any result. It will delete/remove the whole string.
I want only this part: 445288012218368010 (it will be dynamic, so yeah it will be not the same numbers).
Anyway it is for the discord chat bot and I know that there is other methods for check the mentioned names but I want to do that in regex because which I am trying to do can't go the common method.
So yeah how can I get the value from between those symbols?
I need this in node.js regex.
You can use String#match which will return regular expression matches for the string (in this case the RegExp would be <#(\d+)> (the parenthesis around the \d+ make \d+ become its own group). This way you can use <string>.match(/<#(\d+)>/) to get the regular expression results and <string>.match(/<#(\d+)>/)[1] to get the first group of the regex (in this case the number).
You regex matches but you use a non capturing group (?:.*) so you get the full match and replace that with an empty string. Note that you could omit the first and the third non capturing group and use <# and > instead.
You could match what is between the brackets using a capturing group ([^>]+) or (\d+) and use replace and refer the first capturing group $1 in the replacement.
console.log("<#445288012218368010>".replace(/^<#([^>]+)>$/gim, '$1'));
I have a string like hello /world today/
I need to replace /world today/ with /MY NEW STRING/
Reading the manual I have found
newString = string.match("hello /world today/","%b//")
which I can use with gsub to replace, but I wondered is there also an elegant way to return just the text between the /, I know I could just trim it, but I wondered if there was a pattern.
Try something like one of the following:
slashed_text = string.match("hello /world today/", "/([^/]*)/")
slashed_text = string.match("hello /world today/", "/(.-)/")
slashed_text = string.match("hello /world today/", "/(.*)/")
This works because string.match returns any captures from the pattern, or the entire matched text if there are no captures. The key then is to make sure that the pattern has the right amount of greediness, remembering that Lua patterns are not a complete regular expression language.
The first two should match the same texts. In the first, I've expressly required that the pattern match as many non-slashes as possible. The second (thanks lhf) matches the shortest span of any characters at all followed by a slash. The third is greedier, it matches the longest span of characters that can still be followed by a slash.
The %b// in the original question doesn't have any advantages over /.-/ since the the two delimiters are the same character.
Edit: Added a pattern suggested by lhf, and more explanations.