/    Sign up×
Community /Pin to ProfileBookmark

Regex more than matching multiple lines

I had came across this kind of modifier in regex.

“/x” and it’s the little x.

What it does is, it matches a whole pattern across multiple lines, but not [I]that[/I] kind of multiple lines. It’s like a pattern.


/
=+ #lots of equal signs
.*
=+
/x

You basically write a pattern like that and it would match a piece of text like this:

=====

blah
=====

What I want to know is that, what if theres white space in between the

====

Blah

====

would it still match it? Or do you need a whitespace operator? And if you do need a whitespace operator, what measures need to be taken if there are multiple lines of whitespace? like

====

Blah

====

Thanks

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 29.2007 — Have you looked in the manual?

[I] If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.[/I]
Copy linkTweet thisAlerts:
@DragonkaiauthorNov 30.2007 — Well that's the problem, I don't understand what it's saying. Does it just mean that it ignores whitespace unless otherwise noted. Therefore it should match what I said before right? And if I have tons of whitespace lines then it would still match?
Copy linkTweet thisAlerts:
@NogDogNov 30.2007 — Well that's the problem, I don't understand what it's saying. Does it just mean that it ignores whitespace unless otherwise noted. Therefore it should match what I said before right? And if I have tons of whitespace lines then it would still match?[/QUOTE]
It's sort of the inverse of that: white-space within the regular expression is ignored ([i]not white-space in the text being searched) except when escaped or within a character class. Therefore, the following regular expressions are equivalent (if silly in the second case):

<i>
</i>'/this is a test/'
'/t h i s is a t e s t/x'

A more practical use is in complicated regular expressions with nested sub-patterns, assertions, etc. to make them easier to read.
Copy linkTweet thisAlerts:
@DragonkaiauthorDec 01.2007 — Oh, that makes sense now. So your saying that the whitespace in the actual text won't be ignored it's the whitespace in the regex pattern, and it's used to help us look at the pattern easier if there's sub patterns and all that.

ok, thanks, but now I got a bigger problem, if the "/x" doesn't match that kind of lines what does?

is it just the "/m" part?

if so, what kind of pattern can match this:

"
====



blah


====

"

so that the match returns the blah, would there be alot of white space operators? What if, there are unknown amount of white space lines? Im not talking about this:

" " kind of whitespace where it's in one line but this kind:

"

" which is in multiple lines, and that there is an unknown amount of them.
Copy linkTweet thisAlerts:
@bokehDec 01.2007 — [B]^[/B] and [B]$[/B] are both zero width assertions that assert (match) at the start and end of the target string; using the "m" modifier allows them to also match the start and end of a line.
Copy linkTweet thisAlerts:
@DragonkaiauthorDec 01.2007 — Alright, but the whitespace operator s when combined with the "*" does that mean it will also match multiple lines of whitespace as well whitespace one after another. " "
Copy linkTweet thisAlerts:
@bokehDec 02.2007 — You really need to use the manual. All this information can be found in it.

[I] By default, a whitespace character is any character that the C library function isspace() recognizes, though it is possible to compile PCRE with alternative character type tables. Normally isspace() matches space, formfeed, newline, carriage return, horizontal tab, and vertical tab.[/I]
Copy linkTweet thisAlerts:
@DragonkaiauthorDec 02.2007 — Ok, so all I'm asking is will there be any difference in the regex pattern between

"
===



blah
===


"

and "==== blah===="
Copy linkTweet thisAlerts:
@NogDogDec 02.2007 — If you wanted to match on either of those possibilities, then your regexp would be something like:
<i>
</i>'/={3,4}s*blahs*={3,4}/'

(The {3,4} would be modified as needed to represent the minimum and maximum number of "=" characters you would require to be present.)
Copy linkTweet thisAlerts:
@DragonkaiauthorDec 03.2007 — oh right, thanks.
×

Success!

Help @Dragonkai 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.23,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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