Little regex help needed

RonaldS

New member
Jun 17, 2010
288
1
0
I have the following string.
hello babby, how are yyou

I want to select the 'abb' from baby..... but I want to do this very specifically. Right now Im using
Code:
\bb(\w*)\b
The problem is that I do not want the initial B from babby. I need to get regex to select the word/character AFTER the initial B, and the word/character BEFORE the final Y. so that i still get ABB (excluding the B and Y from BABBY)


Let me know
 


You're not being very clear about what you want -- For one, you said "The final Y", when I think you were referring to the "Y" in "babby", which is actually the first "Y" in your string. Also, you don't specify what you're trying to match, except in this one example; hardcoding it, for example, to match "babby" every time, wouldn't really help you with much.

This will find "b(one-or-more-letters)y". I think this is what you're asking for --
Code:
 /b(\w+)y/
\w means any-letter-or-number-but-not-symbol-or-space

Note that this example uses a capture group (e.g. you don't really want the whole word "babby", you just want the part in parentheses "abb", but you want to specify what comes before-and-after it); Your function will probably return an array, where index 0 is the full string "babby", and index 1 is the first capture group "abb". If I knew what language you were using, I could show you how to get the result specifically, but you should be close enough now to figure it out.
 
You're not being very clear about what you want -- For one, you said "The final Y", when I think you were referring to the "Y" in "babby", which is actually the first "Y" in your string. Also, you don't specify what you're trying to match, except in this one example; hardcoding it, for example, to match "babby" every time, wouldn't really help you with much.

This will find "b(one-or-more-letters)y". I think this is what you're asking for --
Code:
 /b(\w+)y/
\w means any-letter-or-number-but-not-symbol-or-space

Note that this example uses a capture group (e.g. you don't really want the whole word "babby", you just want the part in parentheses "abb", but you want to specify what comes before-and-after it); Your function will probably return an array, where index 0 is the full string "babby", and index 1 is the first capture group "abb". If I knew what language you were using, I could show you how to get the result specifically, but you should be close enough now to figure it out.
hey man, thanks for the advice, but i tried your code and it still does not work.
what i really need to do is to get the day from the date.
thus from mm/dd/yyyy i need to get only dd. now i need to regex such that the character after the first '/' and the character before the last '/' is taken.

any ideas?
 
Code:
/(\d{2})\/\d{2}\/\d{4}/
== "(two digits)/two digits/four digits", so you'll still want the first capture group, but e.g. it won't match fractions or date partials ("10/15") because it requires the full mm/dd/yyyy string, to your specification.
 
Code:
/(\d{2})\/\d{2}\/\d{4}/
== "(two digits)/two digits/four digits", so you'll still want the first capture group, but e.g. it won't match fractions or date partials ("10/15") because it requires the full mm/dd/yyyy string, to your specification.

Hey man are you sure this will help me select dd from mm/dd/yyyy ???
Eg: from 02/27/1974 it should give me only 27
 
Hey man are you sure this will help me select dd from mm/dd/yyyy ???
Eg: from 02/27/1974 it should give me only 27

PHP:
  <?php
// Delimiters may be slash, dot, or hyphen
$date = "04/30/1973";
list($month, $day, $year) = split('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br />\n";
?>

assuming you have the date on its own.

Otherwise to modify uplinked's:

Code:
/(\d{2})\/(\d{2})\/\d{4}/

just pulls out the day