Whip
Whip

Reputation: 2234

Regex - Select "<?" but not "<?php"

I'm trying to use regex to select instances where something like <? (a php construct is used but not <?php. I've tried several iterations on regextester but have failed. Here's the latest <\?(?!<\?php)

Basically this is what I want. it covers all the variations in my document

<?php foo ?> //should not match
<? bar ?> //should match '<?'
<?=foobar ?> //should match '<?='
<?xml barbar ?> //should not match

I'm new to regex so any help would be appreciated

Edit: With the problems with the answers posted I'm adding one more condition to match

<?php foo ?> //should not match
<? bar ?> //should match '<?'
<?bar ?> //should match '<?' there could be any character after ?
<?=foobar ?> //should match '<?='
<?xml barbar ?> //should not match

To summarize, I'm only trying to match <? or <?= not the complete line they occur in.

Edit 2: Basically the logic of the expression should be: match <? or <?= but not if followed by `php' or 'xml'

Upvotes: 0

Views: 53

Answers (4)

anubhava
anubhava

Reputation: 785651

You can use a negative lookahead assertion:

<\?=?(?!php|xml)

(?!php|xml) will fail the match if there is php or xml text after <? ir ,?=, thus failing <?php and <?xml.

RegEx Demo

Upvotes: 1

Whip
Whip

Reputation: 2234

I finally found it.

<\?=?(?!php)(?!xml)

This will match <? with an optional = not followed by the string 'php' and 'xml'

Upvotes: 0

Ravi Hirani
Ravi Hirani

Reputation: 6539

Use below regex:-

(<\?(?=\s).*)|(<\?=.*)

It will match both record

<? bar ?> 
<?=foobar ?> 

If you write only

(<\?(?=\s))|(<\?=)

the it will match <? and <?= only

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627128

You can use

<\?[^\w\s]+|<\?\B

See the regex demo

Pattern details:

  • <\?[^\w\s]+ - a literal <? sequence followed with 1+ characters other than a word and whitespace
  • | - or
  • <\?\B - <? literal character sequence followed with a non-word boundary (meaning there should be a non-word character right after ?).

Upvotes: 1

Related Questions