0

正規表現を使用して、前に特定の単語 (この場合は「スライド」と「タイトル」) があるテキストと一致させようとしています。後読みアサーションを使用して相互にネストしようとしましたが、機能していないようです。もう 1 つの問題は、一致させるために使用している「スライド」と「タイトル」の単語の間に別のテキストがあることです。

私はもう試した:

(?<=slide(?<=title\\s=\\s)).*(?=\\\")
(?<=title\\s=\\s(?<=slide)).*(?=\\\")

これを行う方法に関する提案はありますか?余分なスラッシュはエスケープ用です。私はこれをobjective-cで使用していますが、それが重要かどうかはわかりません。

より良いコンテキストのためにスクレイピングしている JSON の一部 (各スライドの「タイトル」の後にタイトルを取得しようとしています):

slide =                 {
                createdAt = "2013-06-18T20:06:50Z";
                description = "<p>Due to the amount of attention paid to each organization's top prospects and early-round draft picks, many of the game's underrated prospects are perpetually obscured. Most of the time, these prospects are younger players who are housed in the low minors and still require considerable physical projection. At the same time, there are countless prospects on the older side of the age curve who have dipped off the radar due to injury.</p><p>Here's a look at one \"hidden gem\" from each organization who could make a push for the major leagues in the coming years.</p>";
                embedCode = "";
                externalId = "<null>";
                id = 3219926;
                photoHasCropExact = 1;
                photoHasCropNorth = 0;
                primaryPhoto =                     {
                    url = "http://img.bleacherreport.net/img/slides/photos/003/219/926/hi-res-5382332_crop_north.jpg";
                };
                title = "Each MLB Team's 'Hidden Gem' Prospect Fans May Not Know About";
                updatedAt = "2013-06-18T22:26:30Z";
                url = "<null>";
4

1 に答える 1

1

これについては@MartinRに同意しますが、正規表現の質問に答えるには、実際に不可能な条件を指定しているためです。あなたが意味するのは

(?<=(?<=title\\s=\\s)slide).*(?=\\\")
(?<=(?<=slide)title\\s=\\s).*(?=\\\")

理由を確認するには、次の正規表現を検討してください。

(?<=foo(?<=bar)).

「foo」が前にあり、「bar」が前にある文字を探しています。もちろん、"foo" と "bar" は決して同じではないため、その条件が一致することはありません。「foo」の前に「bar」を置きたい場合は、次のようにする必要があります。

(?<=(?<=bar)foo).

また、ほとんどの場合、正規表現エンジンは可変幅の後読みをサポートしていないことに注意してください。例には固定幅の後読みのみが含まれていますが、実際の実装がより複雑な場合、正規表現が機能しない別の理由になる可能性があります。

于 2013-06-19T21:42:05.367 に答える