Regex pattern with subpattern exceptions (Python)
By : Andrew Jones
Date : March 29 2020, 07:55 AM
it should still fix some issue I am using BeautifulSoup to extract tabledata tags from a table. The TD's have a class of either 'a','u','e','available-unavailable' or 'unavailable-available'. (Yes, I know quirky class names but hey...) code :
table.findAll('td', attrs = {"class":re.compile(r'(^|\s)(a|unavailable-available)($|\s)')})
class="a"
class="a ui-xxx"
class="ui-xxx a"
class="ui-xxx a ui-yyy"
class="unavailable-available"
class="unavailable-available foo"
|
Is it possible to reuse a subpattern inside of a pattern in Regex
By : user2716191
Date : March 29 2020, 07:55 AM
Does that help I am trying to write a Regex in C# but I have similar constructions that I would like not to duplicate but to reuse. code :
^([A-Z][a-z]+(?:\s+|$)){3}$
|
Find lines that match a pattern with parameterized subpattern, but keep only the first hit for each subpattern
By : Arash Aryani
Date : March 29 2020, 07:55 AM
I hope this helps . The title is a bit long. , This might work for you: code :
sed '/^X/d' file | sort -uk1,1
|
Regular Expressions - match pattern but return subpattern
By : Marcin Janiszewski
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You have just to use capturing groups by using parentheses in your regex: code :
Amount: (\d*) - Class (\d*)
^--^-- Here---^---^
String str = "0 - Amount: 3 - Class 29\n1 - Amount: 2 - Class 21\n2 - Amount: 11 - Class 1";
Pattern pattern = Pattern.compile("Amount: (\\d*) - Class (\\d*)");
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer("{");
while (matcher.find()){
sb.append(String.format("{%s, %s},", matcher.group(1), matcher.group(2)));
}
sb.setLength(sb.length()-1); // remove last comma
sb.append("}");
System.out.println(sb.toString())
|
How to find a pattern that does not contains a subpattern?
By : Kushagra Kumar
Date : March 29 2020, 07:55 AM
hop of those help? Use a negative lookahead as I have already mentioned, just use a consuming subpattern instead of the \1 (that was consuming text in your original regex). Also, do not forget that to match a literal dot, you need to escape it. code :
ps\.set.*\(3, c0\.get\((.*)\)\);(\n.*){3}.*ps\.set.*\(7, c1\.get\(t\)\.get\((?!\1\))[^()]*\)\);\n.*ps\.addBatch\(\);
^^^^^^^^^^^^
|