regex

One Regular Expression to validate US and Canada ZIP / Postal Code

ⅰ亾dé卋堺 提交于 2021-02-07 05:37:05
问题 I am developing a stationery program. Customers have choice to pick their region either US or Canada. When they enter address they have to enter ZIP/Postal code. I am trying to validate field but I cannot use reg exp either for US or Canada. I require a regular expression that validates for both country zip code. 回答1: Not knowing what language you're using, I will not use any abbreviations for character classes: ^[0-9]{5}$|^[A-Z][0-9][A-Z] ?[0-9][A-Z][0-9]$ Depending on your language, you

Regex URL Path from URL

僤鯓⒐⒋嵵緔 提交于 2021-02-07 05:20:10
问题 I am having a little bit of regex trouble. I am trying to get the path in this url videoplay . http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello If I use this regex /.+ it matches /video as well. I would need some kind of anti / negative match to not include // 回答1: In case if you need this for your JavaScript web-app: the best answer I ever found on this topic is here. Basic (and also original) version of the code looks like this: var parser = document

Regex URL Path from URL

自作多情 提交于 2021-02-07 05:18:35
问题 I am having a little bit of regex trouble. I am trying to get the path in this url videoplay . http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello If I use this regex /.+ it matches /video as well. I would need some kind of anti / negative match to not include // 回答1: In case if you need this for your JavaScript web-app: the best answer I ever found on this topic is here. Basic (and also original) version of the code looks like this: var parser = document

Regex URL Path from URL

☆樱花仙子☆ 提交于 2021-02-07 05:18:33
问题 I am having a little bit of regex trouble. I am trying to get the path in this url videoplay . http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello If I use this regex /.+ it matches /video as well. I would need some kind of anti / negative match to not include // 回答1: In case if you need this for your JavaScript web-app: the best answer I ever found on this topic is here. Basic (and also original) version of the code looks like this: var parser = document

Remove new line characters from data recieved from node event process.stdin.on(“data”)

女生的网名这么多〃 提交于 2021-02-07 05:12:13
问题 I've been looking for an answer to this, but whatever method I use it just doesn't seem to cut off the new line character at the end of my string. Here is my code, I've attempted to use str.replace() to get rid of the new line characters as it seems to be the standard answer for this problem: process.stdin.on("data", function(data) { var str; str = data.toString(); str.replace(/\r?\n|\r/g, " "); return console.log("user typed: " + str + str + str); }); I've repeated the str object three times

Remove new line characters from data recieved from node event process.stdin.on(“data”)

天涯浪子 提交于 2021-02-07 05:10:39
问题 I've been looking for an answer to this, but whatever method I use it just doesn't seem to cut off the new line character at the end of my string. Here is my code, I've attempted to use str.replace() to get rid of the new line characters as it seems to be the standard answer for this problem: process.stdin.on("data", function(data) { var str; str = data.toString(); str.replace(/\r?\n|\r/g, " "); return console.log("user typed: " + str + str + str); }); I've repeated the str object three times

How to remove spaces before and after a string?

自作多情 提交于 2021-02-07 04:43:05
问题 I have two words spirited by space of course, and a lot of spaces before and after, what I need to do is to remove the before and after spaces without the in between once. How can I remove the spaces before and after it? 回答1: You don't need regex for that, use trim(): $words = ' my words '; $words = trim($words); var_dump($words); // string(8) "my words" This function returns a string with whitespace stripped from the beginning and end of str. 回答2: For completeness (as this question is tagged

Regex to replace http AND https links

点点圈 提交于 2021-02-07 04:40:32
问题 I have the following preg_replace() function targeting links: $link = preg_replace( "#http://([\S]+?)#Uis", '<a href="http://\\1">(link)</a>', $link ); It works fine with http links but obviously not with https links. How can I adjust it so that it works with https links as well. 回答1: Just add s? after http and match the whole link, then use the $0 backreference to refer to it from the replacement pattern: $link = preg_replace( "#https?://\S+#i", '<a href="$0">(link)</a>', $link ); See the

'find' using regex with variables

半城伤御伤魂 提交于 2021-02-07 04:17:04
问题 Please, help me with the following: Let it be three files: file-aaa.sh file-bbb.sh file-xxx.sh In a bash script I have variables $a=aaa $b=bbb Now, I want to execute something like: find . -name "file-[$a|$b].sh" and expect to get two files in output. What am I doing wrong? 回答1: You can use this find: find . -name "file-$a.sh" -o -name "file-$b.sh" To combine it into one using -regex option: On OSX: find -E . -regex ".*file-($a|$b)\.txt" On Linux: find . -regextype posix-extended -regex ".

'find' using regex with variables

情到浓时终转凉″ 提交于 2021-02-07 04:16:23
问题 Please, help me with the following: Let it be three files: file-aaa.sh file-bbb.sh file-xxx.sh In a bash script I have variables $a=aaa $b=bbb Now, I want to execute something like: find . -name "file-[$a|$b].sh" and expect to get two files in output. What am I doing wrong? 回答1: You can use this find: find . -name "file-$a.sh" -o -name "file-$b.sh" To combine it into one using -regex option: On OSX: find -E . -regex ".*file-($a|$b)\.txt" On Linux: find . -regextype posix-extended -regex ".