Gregory Taylor’s case was so egregious that an appellate justice likened him to Jean Valjean of Victor Hugo’s “Les Miserables,” so disconcerting that he became the centerpiece of debate in a district attorney’s race, so bewildering that a chapter was devoted to his story in a 2005 book about California’s three-strikes law.

Taylor was a 35-year-old homeless man when he was arrested in 1997 for attempting to break into the kitchen of a Catholic church that served the poor, where he was a regular and occasionally volunteered. He told the arresting officer that he was hungry and wanted something to eat. A priest from the church testified in his defense, saying Taylor was welcome there.

Because of his two prior felony convictions — snatching a purse containing $10 and a bus pass, and an unarmed, unsuccessful attempt to rob a man on the street — Taylor was sentenced to 25 years to life in prison for his third strike.

Yet until a couple of Stanford law students took up his case earlier this year, Taylor languished in a San Luis Obispo prison with little hope of getting out: He was not eligible for parole until 2022.

On Monday, a Los Angeles County Superior Court judge ordered Taylor’s release, ruling on a petition of habeas corpus filed by the students. The pair were part of a Stanford Law School project devoted to helping three-strikes inmates serving lengthy sentences for minor third offenses. So far, the project has won the release of 14 inmates, said Michael Romano, its director.

“Today we are able to correct the past and strike his third strike,” Judge Peter Espinoza said in his ruling vacating Taylor’s original sentence and giving him a new sentence of eight years, which he has already served. He said the law, passed in 1994, produced what he called “unintended and unanticipated consequences” in its early years.

New evidence presented by the students of Taylor’s character, medical history and exposure to domestic violence and trauma as a child meant that he falls “outside the spirit of the three-strikes law,” the judge said.

Taylor, who entered the courtroom with his hands cuffed behind his back and clad in yellow-and-blue prison garb, teared up at the judge’s ruling. Taylor’s mother, who hadn’t seen him since 1997, buried her face in her hands and wept.

“I just want to thank you for giving me another chance,” Taylor said in a timid voice at the end of the hearing.

The students, Gabriel Martinez and Reiko Rogozen, said after the ruling that Taylor’s case was a “perfect example” for thousands of others serving disproportionately harsh prison terms.

They contended in the petition that Taylor had inadequate representation because his public defender failed to investigate his childhood and mental health. They also cited what they called a “glaring error” in the jury instructions: The trial judge instructed the panelists that if circumstantial evidence is equal between the defendant’s guilt and innocence, they should vote for guilt. He should have told them to find him innocent, the students wrote.

In the filing, the students described a hardscrabble youth that led to mental health and drug problems from an early age — circumstances the trial judge on Taylor’s case could have considered to dismiss a prior strike. He began showing symptoms of schizophrenia in his middle school years and “self-medicated” with drugs, according to the petition.

But after serving two years in prison for his first two felonies in the mid-1980s, he led a peaceful if bare-bones life on the streets, the students wrote. He attended St. Joseph’s Church and befriended Father Alan McCoy, who allowed him to sleep there.

At Taylor’s sentencing, McCoy pleaded that he not be given a life sentence, saying he was a “very good person who may have made mistakes.”

The two men continued their friendship even after Taylor was sent to prison, corresponding through letters. Earlier this year, when the students came to talk to Taylor about his case, he asked about the priest, saying he hadn’t heard from him in a few months.

The students searched for McCoy on the Internet and found his obituary; he had passed away in late 2009. When they told Taylor, he broke down crying, they said.

Los Angeles County Dist. Atty. Steve Cooley, who repeatedly cited Taylor’s case in his 2000 campaign against Gil Garcetti, did not oppose the students’ petition. After the hearing, Cooley said the ruling “reflected justice maybe a little long overdue in Mr. Taylor’s case.”

Cooley has said in interviews that Taylor’s case was the inspiration for his office’s current policy, under which prosecutors use their discretion not to seek life sentences unless the “third strike” involves a violent or serious crime.

Taylor’s case was highlighted in “Cruel Justice,” a book by journalist Joe Domanick about the three-strikes law.

victoria.kim@latimes.com