Regex - matching characters that are wrapped inside apostrophes or speech marks

Hello,

I was wondering if you may be able to help me with the following regex problems please?

I use https://regex101.com to check my regexes.

The first one is:

/[A-z0-9.=?:()\s]+/g

This is to only allow any-cased letters, numbers and the . ? : ( ) characters and spaces.

However, this regex also matches a backslash. Probably other characters but I haven’t checked. How come?

The second one is:

I’m trying to match question marks or colons only if they are wrapped in apostrophes or speech marks.

(([^’])(?|:)([^’]))

My attempts are just all over the place. I’ve tried various versions of the above, even tried using \B and \b.

I should see a match on something like this:

‘some text here ?: these are matched’ some text here ?: these are NOT matched “some text here ?: these are matched”

Although I see it’s going to be awkward to make the regex ignore text between the two some text here [color=blue]?:[/color] these are matched statements.

My PHP book and the Internet are not revealing the answer so far!

A bit of background:

This is part of the first class I’m writing to make calls to my database. One of the parameters my constructor accepts is $numberOfBinds, i.e. how many values will be bound to the PDO statement.

The second regex is to check that the number of binds matches the number of ?'s or :parameters that are not contained within apostrophes or speech marks in the SQL.

I’ve always liked doing things the hard way! I figure it gives me a broader knowledge as I rampantly trawl through Google for hours before giving up and coming here :slight_smile:

Which problem is the problem?

The regex?

OR

One of the parameters my constructor accepts is $numberOfBinds, i.e. how many values will be bound to the PDO statement.

Cause the second, looks like you are attempting this incorrectly.

The first problem is separate from the second problem. The first problem is for validation of another variable.

I appear to have confused both myself and you about the second problem, my bad!

Can I start again on explaining the second problem? I have modified my plan a bit as well:

I’m creating a class to make calls to my database, passing $queryType, $sql through the constructor.

I need to determine the type of bind (bindValue or bindParam), the number of values to be bound and whether the constructor has received the correct number of parameters (2 + number of binds).

To do all of the above, I need to count the number of…

[ol][li]? (question marks) that are not enclosed within speech marks or apostrophes (or a mixture of both).[/li]
[li]: (colons) that are not enclosed within speech marks or apostrophes (or a mixture of both).[/li][/ol]

…which I believe I can achieve with the preg_match_all function.

If I can make this work with a regex that’d be great as I think it’d be most efficient as I can remove some other validation checks.

My concern is that from a human’s perspective, we assign which speech mark or apostrophe is designated as the opening or closing speech mark or apostrophe around text.

For example:

some text “quoted text” some text “quoted text”

I interpret the blue text as the quoted text, but the regex may interpret the brown text as the quoted text.

I think I’ve found a regex (that I’ve modified slightly) which overcomes this (see 1.png attached).

Working from this, I’ve tried what you see in 2.png.

Theoretically I expected this to match any ? but not if enclosed inside quotes, but it’s not working.

I know there might be an easier way to do what I intend, but it’d be interesting to see what magic piece of regex can do this, as it seems to be escaping \me.


I'm creating a class to make calls to my database, passing $queryType, $sql through the constructor.
This is actually an issue. It will give you grief, and should not be done like that.

Read through this first: http://www.startutorial.com/articles/view/php-crud-tutorial-part-1/

Not really sure why you are wanting to do a wrapper function around the wrapper, but this would be you class. If you want to use question marks or named placed holders you can choose which you want implements. The function can handle that portion and should be separate from the user. Or you go with a querybuilder such as Doctrine.

Thank you for helping.

I don’t know enough to understand right now how your answer will save me time/grief. I had to submit defeat on the regex for what I wanted, and that is when (of course) I found exactly what I was looking for!

(?!\B"[^"])?(?![^"]"\B)

Your help is always appreciated and I don’t intend on frustrating you because I don’t understand it.

Let me try to explain.

You want a separte class for each table. In that file you want to define methods to extract the data you want from the table.

So,

[php]class User extends Model
{
// removed extra methods
public function getUsernameByID( $username ){
$stmt = $this->pdo->prepare(“SELECT userID FROM user WHERE username = ?”);
$stmt->execute([$username]);
return $stmt->fetch();
}
}[/php]

}

Sponsor our Newsletter | Privacy Policy | Terms of Service