43

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.

Topera
  • 12,223
  • 15
  • 67
  • 104
Lukasz
  • 926
  • 3
  • 14
  • 22
  • In which language ? there are a few varieties of Regex you see. – Russ Clarke Oct 17 '11 at 12:49
  • 1
    This is a classic http://www.regular-expressions.info/tutorial.html – xanatos Oct 17 '11 at 12:55
  • first, it is not hard. and, you need to point out in which situation your problem is. – Ya Zhuang Oct 17 '11 at 12:56
  • @RussC: In this case, the language isn't all that relevant because that kind of regex is so basic (and doesn't need any of those newfangled non-regular regex extensions), it'll work in more or less any regex engine, even POSIX BREs. Of course, the OP still needs to know how to apply the regex to his string, so you're still right, of course. – Tim Pietzcker Oct 17 '11 at 13:28

8 Answers8

97
^[^_]*_

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

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
7

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);
}

...

Fred
  • 4,846
  • 1
  • 23
  • 21
4

no need to do a replacement. the regex will give you what u wanted directly:

"(?<=_)[^_]*\.jpg"

tested with grep:

 echo "3.04_somename.jpg"|grep -oP "(?<=_)[^_]*\.jpg"
somename.jpg
Kent
  • 189,393
  • 32
  • 233
  • 301
  • Just what I was looking for, thanks! I'll give these suggestions a try. Working with Perl regex here. – greenage Aug 09 '19 at 21:23
3

Variant of Tim's one, good only on some implementations of Regex: ^.*?_

var subjectString = "3.04_somename.jpg";
var resultString = Regex.Replace(subjectString,
    @"^   # Match start of string
    .*?   # Lazily match any character, trying to stop when the next condition becomes true
    _     # Match the underscore", "", RegexOptions.IgnorePatternWhitespace);
xanatos
  • 109,618
  • 12
  • 197
  • 280
2

In Javascript I would use /.*_/, meaning: match everything until _ (including)

Example:

console.log( 'hello_world'.replace(/.*_/,'') ) // 'world'
Fabiano Soriani
  • 8,182
  • 9
  • 43
  • 59
1

For Notepad++ users who are getting all but one line's results deleted, try this:

^.*_

Check the box for "Wrap around" Uncheck the box for "matches newline" Click "Replace All"

Source: https://www.winhelponline.com/blog/notepad-plus-find-and-replace-text/

A. Kendall
  • 391
  • 3
  • 12
0

If you cast your url as [System.Uri], the localPath field gives you what you want:

$link_url = "https://www.subdomain.domain.com/files
$local_path= [System.Uri]::new($link_url)

Will yield $local_path = /files

0

I learned all my Regex from this website: http://www.zytrax.com/tech/web/regex.htm. Google on 'Regex tutorials' and you'll find loads of helful articles.

String regex = "[a-zA-Z]*\.jpg";
System.out.println ("somthing.jpg".matches (regex));

returns true.

Hidde
  • 11,493
  • 8
  • 43
  • 68