Am embarrassed to ask this but RegEx is really my kryptonite.

I have a RegEx line that extracts just numbers from a string.

However, I need to tell it that I only want the numbers that come BEFORE a specific set of characters, let's say SDCSDC. Any numbers after that I do not want.

Can someone please help me? Thanks ahead.

Try this,

$name=qq{your text 100 SDCSDC 1000 some text 101 SDCSDC 1000 some text 1001specific_word100};

# Declare your search words
@spec_word=("SDCSDC", "specific_word");

# Search the specific word
foreach $key (@spec_word)
{
	push @$key, $1 while $name=~ m{(\d+)\s*$key}g;
}

# print the words and their numbers
foreach $key (@spec_word)
{
	print "\n\nList of numbers before : $key";
	print "\n$_" for @$key;
}

I appreciate your reply, but my string is one long string, with no set delimiter. There might or might NOT be spaces.

Currently I use this:

$value =~ s/\D//g;

I simply need to modify it so that anything after SDCSDC is dropped, whether it's a number or not.

Is this possible in RegEx?

Although your post gave me the idea to use the SPLIT function to split the string using SDCSDC as a delimiter, so thanks for that.

But would love to know if it can be done within RegEx

Currently I use this: $value =~ s/\D//g; I simply need to modify it so that anything after SDCSDC is dropped, whether it's a number or not.

Is this possible in RegEx?

You can modify the RegEx you were using from s/\D//g; into s/(SDCSDC.*$|\D)//g;

#!/usr/bin/perl -w
use strict;

#Here is my guess of what one of your long strings might look like
my $value = "The first digit is 3, the second digit is 4, the third is 2, and the SDCSDC rest of the digits 4 in the substring to be eliminated 9 4862 will not be included in the resulting value.";

#Eliminate SDCSDC and all other characters to the end of the string
# as well as eliminating all non-digit characters
$value =~ s/(SDCSDC.*$|\D)//g;

print "The value of the portion of the string to the left of SDCSDC is: $value \n";
This article has been dead for over six months. Start a new discussion instead.