Is there an easy way to remove all chars before a "_"? For example, change 3.04_somename.jpg
to somename.jpg
.
Any suggestions for where to learn to write regex would be great too. Most places I check are hard to learn from.
$ means "Match the end of the string" (the position after the last character in the string).
. trim() removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).
This answer is not useful. Show activity on this post. [] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
^[^_]*_
will match all text up to the first underscore. Replace that with the empty string.
For example, in C#:
resultString = Regex.Replace(subjectString, @"^ # Match start of string [^_]* # Match 0 or more characters except underscore _ # Match the underscore", "", RegexOptions.IgnorePatternWhitespace);
For learning regexes, take a look at http://www.regular-expressions.info
The regular expression:
^[^_]*_(.*)$
Then get the part between parenthesis. In perl:
my var = "3.04_somename.jpg"; $var =~ m/^[^_]*_(.*)$/; my fileName = $1;
In Java:
String var = "3.04_somename.jpg"; String fileName = ""; Pattern pattern = Pattern.compile("^[^_]*_(.*)$"); Matcher matcher = pattern.matcher(var); if (matcher.matches()) { fileName = matcher.group(1); }
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With