regex

Replace comma or whitespace with hyphen in same string

心已入冬 提交于 2021-02-16 13:58:07
问题 I need PHP code to replace comma or whitespace with hyphen For eg: If $value = 'home garden' or $value = 'home,garden' , i need result as home-garden I tried $result = preg_replace('/\s+[\,]/', '-', trim($value)); , but no use.. Can someone explain it? 回答1: $result = preg_replace('/[ ,]+/', '-', trim($value)); Test: $value = ' home ,garden , gardener '; $result = preg_replace('/[ ,]+/', '-', trim($value)); echo $result; //home-garden-gardener 回答2: $result = str_replace(array(',', ' '), '-',

Exclude strings within parentheses from a regular expression?

不打扰是莪最后的温柔 提交于 2021-02-16 13:40:08
问题 I'm looking to split space-delimited strings into a series of search terms. However, in doing so I'd like to ignore spaces within parentheses. For example, I'd like to be able to split the string a, b, c, search:(1, 2, 3), d into [[a] [b] [c] [search:(1, 2, 3)] [d]] Does anyone know how to do this using regular expressions in Java? Thanks! 回答1: This isn't a full regex, but it'll get you there: (\([^)]*\)|\S)* This uses a common trick, treating one long string of characters as if it were a

Javascript Remove all charater except leading - , one dot and digits

泪湿孤枕 提交于 2021-02-16 13:15:21
问题 First of all this question is not same as strip non-numeric characters from string or Regex to replace everything except numbers and a decimal point I want to convert a string with valid number like. --1234// will be -1234 -123-123 will be -123123 12.123.3 will be 12.1233 -123.13.123 will be -123.13123 I tried those number.replace(/[^0-9.-]/g, '') //it accepts multiple . and - number.replace(/[^0-9.]-/g, '').replace(/(\..*)\./g, '$1');//it accepts multiple minus I am facing Problem with

Javascript Remove all charater except leading - , one dot and digits

霸气de小男生 提交于 2021-02-16 13:14:36
问题 First of all this question is not same as strip non-numeric characters from string or Regex to replace everything except numbers and a decimal point I want to convert a string with valid number like. --1234// will be -1234 -123-123 will be -123123 12.123.3 will be 12.1233 -123.13.123 will be -123.13123 I tried those number.replace(/[^0-9.-]/g, '') //it accepts multiple . and - number.replace(/[^0-9.]-/g, '').replace(/(\..*)\./g, '$1');//it accepts multiple minus I am facing Problem with

A strict regular expression for matching chemical formulae

早过忘川 提交于 2021-02-16 13:09:46
问题 In the course of processing a large textual chemical database with Perl, I had been faced with the problem of using a regex to match chemical formulae. I have seen these two previous topics, but the suggested answers there are too loose for my requirements. Specifically, my (admittedly limited) research has led me to this posting that gives a regex for the currently accepted chemical symbols, which I'll copy here for reference [BCFHIKNOPSUVWY]|[ISZ][nr]|[ACELP][ru]|A[cglmst]|B[aehikr]|C

A strict regular expression for matching chemical formulae

自闭症网瘾萝莉.ら 提交于 2021-02-16 13:06:28
问题 In the course of processing a large textual chemical database with Perl, I had been faced with the problem of using a regex to match chemical formulae. I have seen these two previous topics, but the suggested answers there are too loose for my requirements. Specifically, my (admittedly limited) research has led me to this posting that gives a regex for the currently accepted chemical symbols, which I'll copy here for reference [BCFHIKNOPSUVWY]|[ISZ][nr]|[ACELP][ru]|A[cglmst]|B[aehikr]|C

A strict regular expression for matching chemical formulae

淺唱寂寞╮ 提交于 2021-02-16 13:06:03
问题 In the course of processing a large textual chemical database with Perl, I had been faced with the problem of using a regex to match chemical formulae. I have seen these two previous topics, but the suggested answers there are too loose for my requirements. Specifically, my (admittedly limited) research has led me to this posting that gives a regex for the currently accepted chemical symbols, which I'll copy here for reference [BCFHIKNOPSUVWY]|[ISZ][nr]|[ACELP][ru]|A[cglmst]|B[aehikr]|C

RegEx to match a pattern, as long as it is not preceded by a different pattern

送分小仙女□ 提交于 2021-02-16 10:24:27
问题 I need a regex that is to be used for text substitution. Example: text to be matched is ABC (which could be surrounded by square brackets), substitution text is DEF . This is basic enough. The complication is that I don't want to match the ABC text when it is preceded by the pattern \[[\d ]+\]\. - in other words, when it is preceded by a word or set of words in brackets, followed by a period. Here are some examples of source text to be matched, and the result, after the regex substitution

RegEx to match a pattern, as long as it is not preceded by a different pattern

核能气质少年 提交于 2021-02-16 10:24:26
问题 I need a regex that is to be used for text substitution. Example: text to be matched is ABC (which could be surrounded by square brackets), substitution text is DEF . This is basic enough. The complication is that I don't want to match the ABC text when it is preceded by the pattern \[[\d ]+\]\. - in other words, when it is preceded by a word or set of words in brackets, followed by a period. Here are some examples of source text to be matched, and the result, after the regex substitution

need to get everything after 2nd dash in string?

安稳与你 提交于 2021-02-16 09:11:21
问题 I have string values like those below: string str1 = "123-456-test"; string str1 = "123 - 456 - test-test"; string str1 = "123-REQ456-test"; string str1 = "123 - REQ456 - test-test"; I need to grab the entire content from string right after the 2nd dash. I tried String.Split('-') , but it did not work. I think I need to use a regex, but I am not able to find a correct one. Please suggest. 回答1: (?:[^-\n]+-){2}(.*)$ You can try this.Grab the capture.See demo. https://regex101.com/r/tS1hW2/21