What special characters must be escaped in regular expressions?

Which characters you must and which you mustn’t escape indeed depends on the regex flavor you’re working with. For PCRE, and most other so-called Perl-compatible flavors, escape these outside character classes: .^$*+?()[{\| and these inside character classes: ^-]\ For POSIX extended regexes (ERE), escape these outside character classes (same as PCRE): .^$*+?()[{\| Escaping any other … Read more

How to input a regex in string.replace?

This tested snippet should do it: import re line = re.sub(r”</?\[\d+>”, “”, line) Edit: Here’s a commented version explaining how it works: line = re.sub(r””” (?x) # Use free-spacing mode. < # Match a literal ‘<‘ /? # Optionally match a “https://stackoverflow.com/” \[ # Match a literal ‘[‘ \d+ # Match one or more digits … Read more

Java string split with “.” (dot) [duplicate]

You need to escape the dot if you want to split on a literal dot: String extensionRemoved = filename.split(“\\.”)[0]; Otherwise you are splitting on the regex ., which means “any character”. Note the double backslash needed to create a single backslash in the regex. You’re getting an ArrayIndexOutOfBoundsException because your input string is just a … Read more

How to extract a substring using regex

Assuming you want the part between single quotes, use this regular expression with a Matcher: “‘(.*?)'” Example: String mydata = “some string with ‘the data i want’ inside”; Pattern pattern = Pattern.compile(“‘(.*?)'”); Matcher matcher = pattern.matcher(mydata); if (matcher.find()) { System.out.println(matcher.group(1)); } Result: the data i want

Remove HTML tags from a String

Use a HTML parser instead of regex. This is dead simple with Jsoup. public static String html2text(String html) { return Jsoup.parse(html).text(); } Jsoup also supports removing HTML tags against a customizable whitelist, which is very useful if you want to allow only e.g. <b>, <i> and <u>. See also: RegEx match open tags except XHTML … Read more

Regex: match everything but a specific pattern

Regex: match everything but: a string starting with a specific pattern (e.g. any – empty, too – string not starting with foo): Lookahead-based solution for NFAs: ^(?!foo).*$ ^(?!foo) Negated character class based solution for regex engines not supporting lookarounds: ^(([^f].{2}|.[^o].|.{2}[^o]).*|.{0,2})$ ^([^f].{2}|.[^o].|.{2}[^o])|^.{0,2}$ a string ending with a specific pattern (say, no world. at the end): Lookbehind-based … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)