Asked  7 Months ago    Answers:  5   Viewed   26 times

I need to match a space character in a PHP regular expression. Anyone got any ideas?

I mean like "gavin schulz", the space in between the two words. I am using a regular expression to make sure that I only allow letters, number and a space. But I'm not sure how to find the space. This is what I have right now:

$newtag = preg_replace("/[^a-zA-Z0-9s|]/", "", $tag);

 Answers

30

If you're looking for a space, that would be " " (one space).

If you're looking for one or more, it's " *" (that's two spaces and an asterisk) or " +" (one space and a plus).

If you're looking for common spacing, use "[ X]" or "[ X][ X]*" or "[ X]+" where X is the physical tab character (and each is preceded by a single space in all those examples).

These will work in every* regex engine I've ever seen (some of which don't even have the one-or-more "+" character, ugh).

If you know you'll be using one of the more modern regex engines, "s" and its variations are the way to go. In addition, I believe word boundaries match start and end of lines as well, important when you're looking for words that may appear without preceding or following spaces.

For PHP specifically, this page may help.

From your edit, it appears you want to remove all non valid characters The start of this is (note the space inside the regex):

$newtag = preg_replace ("/[^a-zA-Z0-9 ]/", "", $tag);
#                                    ^ space here

If you also want trickery to ensure there's only one space between each word and none at the start or end, that's a little more complicated (and probably another question) but the basic idea would be:

$newtag = preg_replace ("/ +/", " ", $tag); # convert all multispaces to space
$newtag = preg_replace ("/^ /", "", $tag);  # remove space from start
$newtag = preg_replace ("/ $/", "", $tag);  # and end
Wednesday, March 31, 2021
 
RahulG
answered 7 Months ago
38

You need to specify a matching group using brackets in order to extract content.

preg_match("//([^/]+)$/", "5020203010101/FIS/CASH FUND/SBG091241212", $matches);

echo $matches[1];
Wednesday, March 31, 2021
 
exxed
answered 7 Months ago
11

I would not recommend using regular expressions for a task like this.
A simple function to check a string for properly written brackets is quickly written:

/**
* @param $str input string
* @returns boolean true if all brackets are properly opened and closed, false otherwise
*/
function checkBraces($str)
{
    $strlen = strlen($str); // cache string length for performance
    $openbraces = 0;

    for ($i = 0; $i < $strlen; $i++)
    {
        $c = $str[$i];
        if ($c == '<') // count opening bracket
            $openbraces++;
        if ($c == '>') // count closing bracket
            $openbraces--;

        if ($openbraces < 0) // check for unopened closing brackets
            return false;
    }

    return $openbraces == 0; // check for unclosed open brackets
}

Using this code as a basis, it shouldn't be too hard to implement a check to verify whether or not the tag name of opening and closing brackets also matches - but I'll leave that to you :-)

Wednesday, March 31, 2021
 
Sauleil
answered 7 Months ago
87

Try a character class instead

echo "$STRING" | egrep '[*]' 
Monday, August 2, 2021
 
Fredy
answered 3 Months ago
69

Use a negative a lookbehind to define a boundary:

(?<!\)(?:\\)*|

See live demo here

Taking care of backslashes in Java, above regex would be:

(?<!\\)(?:\\\\)*\|
Saturday, August 28, 2021
 
James King
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :