regex

Custom Regex Replacement in Java

谁说我不能喝 提交于 2021-01-27 06:17:26
问题 I want to find all patterns in a string that match the regex: no="(\d+)" then replace the digits in the group with something else. In the method below I can find and replace the entire match, but that is not what I want. Is there a way to replace just a portion in that match? private String replaceStringFromPatternSearch(String stringToSearch, String patternString, String replacementString) { String replacedString = null; Pattern stringPattern = Pattern.compile(patternString); Matcher

Regex Alternation Order

蹲街弑〆低调 提交于 2021-01-27 06:03:16
问题 I set up a complex regex to extract data from a page of text. For some reason the order of the alternation is not what I expect. A simple example would be: ((13th|(Executive |Residential)|((\w+) ){1,3})Floor) Put simply I am trying to either get a floor number, a known named floor and, as a back-up, I capture 1-3 unknown words followed by floor just in case to review later (I in fact use a groupname to identify this but didn't want to confuse the issue) The issue is if the string is on the

Regex Alternation Order

白昼怎懂夜的黑 提交于 2021-01-27 06:01:37
问题 I set up a complex regex to extract data from a page of text. For some reason the order of the alternation is not what I expect. A simple example would be: ((13th|(Executive |Residential)|((\w+) ){1,3})Floor) Put simply I am trying to either get a floor number, a known named floor and, as a back-up, I capture 1-3 unknown words followed by floor just in case to review later (I in fact use a groupname to identify this but didn't want to confuse the issue) The issue is if the string is on the

Regex Alternation Order

痞子三分冷 提交于 2021-01-27 06:01:00
问题 I set up a complex regex to extract data from a page of text. For some reason the order of the alternation is not what I expect. A simple example would be: ((13th|(Executive |Residential)|((\w+) ){1,3})Floor) Put simply I am trying to either get a floor number, a known named floor and, as a back-up, I capture 1-3 unknown words followed by floor just in case to review later (I in fact use a groupname to identify this but didn't want to confuse the issue) The issue is if the string is on the

MySQL REGEXP No Whitespaces No Numbers

五迷三道 提交于 2021-01-27 05:45:18
问题 I'm trying to return only those rows which colA and colB do not contain a number or a whitespace Here is my code so far. SELECT * FROM table_name WHERE colA REGEXP '[^0-9^\W]' AND colB REGEXP '[^0-9^\W]' given the dataset colA colB ---------- ---------- test | testB 33 | text <- this is not returned blah | 0123A <- this is returned I am assuming my issue is with my regexp... any help please? 回答1: Well your expression does not work because: 33 is both numbers and you're looking for any non

Contents of a RegExp match

為{幸葍}努か 提交于 2021-01-27 05:43:31
问题 I've been trying to convert the following from Python to node.js. It's a simply program that uses regex to check if an IP address is Public or Private: import re def is_private_ip(ip): """ Returns `True` if the `ip` parameter is a private network address. """ c = re.compile('(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)') if c.match(ip): return True return False print is_private_ip('192.168.0.1') # True print is_private_ip('8.8.8.8') # False print is

Contents of a RegExp match

感情迁移 提交于 2021-01-27 05:42:40
问题 I've been trying to convert the following from Python to node.js. It's a simply program that uses regex to check if an IP address is Public or Private: import re def is_private_ip(ip): """ Returns `True` if the `ip` parameter is a private network address. """ c = re.compile('(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)') if c.match(ip): return True return False print is_private_ip('192.168.0.1') # True print is_private_ip('8.8.8.8') # False print is

parsing a string in python: how to split newlines while ignoring newline inside quotes

随声附和 提交于 2021-01-27 05:42:28
问题 I have a text that i need to parse in python. It is a string where i would like to split it to a list of lines, however, if the newlines (\n) is inside quotes then we should ignore it. for example: abcd efgh ijk\n1234 567"qqqq\n---" 890\n should be parsed into a list of the following lines: abcd efgh ijk 1234 567"qqqq\n---" 890 I've tried to it with split('\n') , but i don't know how to ignore the quotes. Any idea? Thanks! 回答1: Here's a much easier solution. Match groups of (?:"[^"]*"|.)+ .

parsing a string in python: how to split newlines while ignoring newline inside quotes

人盡茶涼 提交于 2021-01-27 05:42:06
问题 I have a text that i need to parse in python. It is a string where i would like to split it to a list of lines, however, if the newlines (\n) is inside quotes then we should ignore it. for example: abcd efgh ijk\n1234 567"qqqq\n---" 890\n should be parsed into a list of the following lines: abcd efgh ijk 1234 567"qqqq\n---" 890 I've tried to it with split('\n') , but i don't know how to ignore the quotes. Any idea? Thanks! 回答1: Here's a much easier solution. Match groups of (?:"[^"]*"|.)+ .

Tiebreaker for same-length regex alternatives with same starting position

寵の児 提交于 2021-01-27 05:34:35
问题 Using GNU sed (with the -r flag for clarity), the following two substitutions on the input string ab give the same result: s/(.)(.)|(.)(.)$/\2\1\3\4/ and s/(.)(.)$|(.)(.)/\1\2\4\3/ both give ba . It would appear that the alternative (.)(.) (the one without $ ) succeeds in both substitutions, regardless of whether its position as the first or second alternative. Why is this the case? What is the tie-breaker for such alternatives? The POSIX specification of regular expressions specifies 1 the