/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Multiline preg match (with out /s)

Ok, so this may seem a tad weird, but I wnat to be able to do a multiline match of a regular expression without using the /s modifier.. The reason is that I want to make a string this this:

text = “sometext”

and have the ability for sometext to be either one line or more than one line.. However when using the /s modifier, it gets “Greedy” and goes or the last ” on the page. So how can I make it match every line inside of the ” “??

Thanks so much in advance,
Kyle

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@bokehJul 31.2008 — [CODE]r?n[/CODE]That will match one new line. By the way using the "s" modifier does not change the greediness of an expression.
Copy linkTweet thisAlerts:
@Kyleva2204authorJul 31.2008 — Could you please be a little more descriptive? I don't want to match just a line, I would like to match all text, and new lines (if there are any).

This is what I have:

'@text = "(.*)"'

I would like for preg to match new lines inside of the ( ), and end at the last " knowing that its the end of the line, and there are no more new lines to follow. ARGH this is difficult to explain, because i have a feeling its impossible to do...

Like, be able to match plainly one line inside of the ( ), but if there is a new line, then have preg go with it.. But stop at the last " which will be at the end of the line....

Thinking about it, I may add ; to the end, just to counteract the greediness a little more..
Copy linkTweet thisAlerts:
@NogDogJul 31.2008 — You could use "*?" instead of "*" to make it un-greedy.
Copy linkTweet thisAlerts:
@bokehJul 31.2008 — Could you please be a little more descriptive?[/QUOTE]It's hard to know what you want to match. Instead of trying to explain post some text and highlight the part you want to match.
Copy linkTweet thisAlerts:
@Kyleva2204authorJul 31.2008 — Sorry this is so confusing, its even confusing for me.. I just made my regular expression more defined.. Like, here is what i was trying to do.. A user would put the following:

@css = "

body {background: black;}

.sometext {font-family: arial}

"

and I wanted PHP to pick up on the contents inside of the quotes.. I just changed the entire thing to:

<!--startcss-->

body {background: black}

.sometext {font-family: arial}

<!--endcss-->

Because the likely hood of the user having <!--endcss--> in their actual page would be very unlikely.
Copy linkTweet thisAlerts:
@bokehJul 31.2008 — So is the problem solved now, or did you still want help?
Copy linkTweet thisAlerts:
@Kyleva2204authorJul 31.2008 — The problem is solved, I just changed the layout.. But I would still like to know if it would be possible.. Look at this example text.


@css = "

body {background: black url("my_site.jpg");}

.mytext {font-family: arial}

"

<div class="mytext">Cornflakes makes for a good "breakfast".</div>
[/QUOTE]

I would like for PHP to match @css = "" and the content inside of the quotes, but have it not continue on past breakfast.. Is this possible?
Copy linkTweet thisAlerts:
@bokehJul 31.2008 — [CODE](?<=@csss=s")(?:{.+}|[^"])+[/CODE]That would match the part highlighted below. Is that what you meant?[CODE]@css = "
[COLOR="Red"][B]body {background: black url("my_site.jpg");}
.mytext {font-family: arial}[/B][/COLOR]
"
<div class="mytext">Cornflakes makes for a good "breakfast".</div>[/CODE]
Copy linkTweet thisAlerts:
@Kyleva2204authorJul 31.2008 — WOW! Nice! haha, you make it looks so easy O_O.. Could you possible explain some aspects of it though, the regEx. There are a lot of things that done make sense to me...

Like why do you have a ? at the beginning of a match.. And waht does the <= mean? Also why are you backslashing quotations? And the :, what does that mean? Why do you ahve the matches where they are?

You dont HAVE to explain it, it would just make it easier on me to learn it..

Thanks a bunch!
Copy linkTweet thisAlerts:
@bokehJul 31.2008 — [CODE](?<=@csss=s")[/CODE]That's a look behind. It's a zero width assertion, i.e. it doesn't capture anything, it just looks backwards up the string to make sure the match will start at the right place. [CODE](?:...)[/CODE]That's a non capturing match, i.e. it does not create a back reference.
Copy linkTweet thisAlerts:
@Kyleva2204authorJul 31.2008 — Awesome! I looked up some advanced stuff, but I cant seem to match the entire @css = " " string, is that possible with your method?
Copy linkTweet thisAlerts:
@bokehJul 31.2008 — Awesome! I looked up some advanced stuff, but I cant seem to match the entire @css = " " string, is that possible with your method?[/QUOTE]Yes! That's why I said highlight what you want to match. It's always a lot clearer than trying to explain in words.

[CODE]@csss=s"(?:{.+}|[^"])+"[/CODE]
Copy linkTweet thisAlerts:
@Kyleva2204authorJul 31.2008 — I mainly wanted just the content inside of the quotes, but I also wanted to replace the entire string with a blank string.. To remove it from the content. So you helped for both! Thanks!
×

Success!

Help @Kyleva2204 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...