How to replace all characters in a Java string with stars

Java 11 and later str = “*”.repeat(str.length()); Note: This replaces newlines \n with *. If you want to preserve \n, see solution below. Java 10 and earlier str = str.replaceAll(“.”, “*”); This preserves newlines. To replace newlines with * as well in Java 10 and earlier, you can use: str = str.replaceAll(“(?s).”, “*”); The (?s) … Read more

How to match repeated patterns?

Try the following: \w+(?:\.\w+)+ The + after (?: … ) tell it to match what is inside the parenthesis one or more times. Note that \w only matches ASCII characters, so a word like café wouldn’t be matches by \w+, let alone words/text containing Unicode. EDIT The difference between […] and (?:…) is that […] … Read more

Regex for password PHP [duplicate]

^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$ From the fine folks over at Zorched. ^: anchored to beginning of string \S*: any set of characters (?=\S{8,}): of at least length 8 (?=\S*[a-z]): containing at least one lowercase letter (?=\S*[A-Z]): and at least one uppercase letter (?=\S*[\d]): and at least one number $: anchored to the end of the string To include … Read more

Using Directory.GetFiles with a regex in C#?

Directory.GetFiles doesn’t support RegEx by default, what you can do is to filter by RegEx on your file list. Take a look at this listing: Regex reg = new Regex(@”^^(?!p_|t_).*”); var files = Directory.GetFiles(yourPath, “*.png; *.jpg; *.gif”) .Where(path => reg.IsMatch(path)) .ToList();

How to remove empty lines from a formatted string

If you also want to remove lines that only contain whitespace, use resultString = Regex.Replace(subjectString, @”^\s+$[\r\n]*”, string.Empty, RegexOptions.Multiline); ^\s+$ will remove everything from the first blank line to the last (in a contiguous block of empty lines), including lines that only contain tabs or spaces. [\r\n]* will then remove the last CRLF (or just LF … Read more

Get group names in java regex

There is no API in Java to obtain the names of the named capturing groups. I think this is a missing feature. The easy way out is to pick out candidate named capturing groups from the pattern, then try to access the named group from the match. In other words, you don’t know the exact … Read more