Take the 2-minute tour ×

I have a string 1/temperatoA,2/CelcieusB!23/33/44,55/66/77 and I would like to extract the words temperatoA and CelcieusB.

I have this regular expression (\d+/(\w+),?)*! but I only get the match 1/temperatoA,2/CelcieusB!


share|improve this question
Which regular expression engine ar you using? –  Johnsyweb Jun 7 '10 at 19:04
You need to accept an answer. –  0x499602D2 Sep 29 '12 at 13:46
add comment

4 Answers

Your whole match evaluates to '1/temperatoA,2/CelcieusB' because that matches the following expression:

qr{ (       # begin group 
      \d+   # at least one digit
      /     # followed by a slash
     (\w+)  # followed by at least one word characters
     ,?     # maybe a comma
    )*      # ANY number of repetitions of this pattern.

'1/temperatoA,' fulfills capture #1 first, but since you are asking the engine to capture as many of those as it can it goes back and finds that the pattern is repeated in '2/CelcieusB' (the comma not being necessary). So the whole match is what you said it is, but what you probably weren't expecting is that '2/CelcieusB' replaces '1/temperatoA,' as $1, so $1 reads '2/CelcieusB'.

Anytime you want to capture anything that fits a certain pattern in a certain string it is always best to use the *g*lobal flag and assign the captures into an array. Since an array is not a single scalar like $1, it can hold all the values that were captured for capture #1.

When I do this:

my $str   = '1/temperatoA,2/CelcieusB!23/33/44,55/66/77';
my $regex = qr{(\d+/(\w+))};
if ( my @matches = $str =~ /$regex/g ) { 
    print Dumper( \@matches );

I get this:

$VAR1 = [

Now, I figure that's probably not what you expected. But '3' and '6' are word characters, and so--coming after a slash--they comply with the expression.

So, if this is an issue, you can change your regex to the equivalent: qr{(\d+/(\p{Alpha}\w*))}, specifying that the first character must be an alpha followed by any number of word characters. Then the dump looks like this:

$VAR1 = [

And if you only want 'temperatoA' or 'CelcieusB', then you're capturing more than you need to and you'll want your regex to be qr{\d+/(\p{Alpha}\w*)}.

However, the secret to capturing more than one chunk in a capture expression is to assign the match to an array, you can then sort through the array to see if it contains the data you want.

share|improve this answer
+1 This looks like a damn fine explanation to me - above and beyond the call of duty. –  Mike Jun 24 '10 at 21:04
add comment

With a Perl-compatible regex engine you can search for


(?<=\d/) asserts that there is a digit and a slash before the start of the match

\w+ matches the identifier. This allows for letters, digits and underscore. If you only want to allow letters, use [A-Za-z]+ instead.

(?=.*!) asserts that there is a ! ahead in the string - i. e. the regex will fail once we have passed the !.

Depending on the language you're using, you might need to escape some of the characters in the regex.

E. g., for use in C (with the PCRE library), you need to escape the backslashes:

myregexp = pcre_compile("(?<=\\d/)\\w+(?=.*!)", 0, &error, &erroroffset, NULL);
share|improve this answer
i use pcrl perl comapatibe regular expression –  farka Jun 8 '10 at 7:07
In which programming language? PCRE is available for many different languages. The good news is that the regex will work because PCRE supports lookaround. –  Tim Pietzcker Jun 8 '10 at 8:03
but not working :-))) –  farka Jun 8 '10 at 8:05
Please answer my question - otherwise there's no way to help you. –  Tim Pietzcker Jun 8 '10 at 8:14
i write programm in c –  farka Jun 8 '10 at 8:56
add comment

The question here is: why are you using a regular expression that’s so obviously wrong? How did you get it?

The expression you want is simply as follows:

share|improve this answer
i bekomme nothing –  farka Jun 7 '10 at 18:41
i use regex , i want only temperatoA and CelcieusB vor ! –  farka Jun 7 '10 at 18:41
@farka: Show us how you’re using the expression. It’s not the expression that’s wrong, it’s how you’re using it. –  Konrad Rudolph Jun 7 '10 at 18:45
i want to extract only words bevor ! and betwenn 1/word1 2/word2 3/word21...n/word –  farka Jun 7 '10 at 19:02
add comment

Will this work?


I made the following assumptions...

  1. A word begins with an alphabetic character.
  2. A word always immediately follows a slash. No intervening spaces, no words in the middle.
  3. Words after the exclamation point are ignored.
  4. You have some sort of loop to capture more than one word. I'm not familiar enough with the C library to give an example.

[[:alpha:]] matches any alphabetic character.

The \b matches a word boundary.

And the (?=.*!) came from Tim Pietzcker's post.

share|improve this answer
add comment

Your Answer


