delete characters?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

delete characters?

Post by jasondavis »

I have a text file like with entries like this
10692336 217] albafghffg@hotmail.com:0943689

I need to fine these entries 1 per line from a text file and delte everything to the left side of
]
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

moved to regex.

you'll need a regular expression to location the section you want to keep and strip out the rest.

if you can tell me what you want to keep, i can help you with the pattern.
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Re: delete characters?

Post by christian_phpbeginner »

jasondavis wrote:I have a text file like with entries like this
10692336 217] albafghffg@hotmail.com:0943689

I need to fine these entries 1 per line from a text file and delte everything to the left side of
]
hi, I wonder if you have found the solution yet. But, what a coincedence, your case is just mine also.

So, I have the code:

Code: Select all

$strTest = "10692336 217] albafghffg@hotmail.com:0943689";

//pay attention on these 2 different patterns in order to do something else different but using the same $strTest
$patternStrTest = '/(\d+\s\d+)\W/';
$patternRetrieveFrontNumber= '/\][a-z\s\@\.\w\:]+/';

preg_match_all($patternStrTest, $strTest, $arrayTest);
foreach($arrayTest as $line) {
   //Output A
   echo "<pre>";
   print_r ($line);
   echo "</pre>";
}

$deleteNum= preg_replace ($patternStrTest, '', $strTest);

//Output B
echo $deleteNum;

$deleteEmail = preg_replace ($patternRetrieveFrontNumber, '', $strTest);

//Output C
echo $deleteEmail;


Output A:

Code: Select all

Array
(
    [0] => 10692336 217]
)

Array
(
    [0] => 10692336 217
)
Output B:

Code: Select all

albafghffg@hotmail.com:0943689
Output C:

Code: Select all

10692336 217
Output C is what you want, I think. Read the tutorial for REGEX, because each time you meet new task with different pattern, then you'll need different pattern of REGEX. So, learning REGEX would be very worthy !

Good luck,
Chris
Post Reply