1

I'm trying to write some regex to match everything apart from a symbol (well two symbols actually). I can get it working on regex101 but when written in PHP it doesn't seem to work correctly.

I want to match anything other than say £ or $.

I've tried this: /[^£$]/g but that only seems to match if the symbol is at the beginning of the string. I've tried so many combinations, tried escaping characters anchoring to the end which has worked a bit better but then it wouldn't catch something like this£inside.

What am I doing wrong?

u_mulder
  • 54,101
  • 5
  • 48
  • 64
Lewis Lebentz
  • 817
  • 4
  • 13
  • 24
  • What exactly do you mean by _"match anything"_? Can you explain what the end-result should look like for any given input? – Phil Jan 28 '18 at 23:34
  • `preg_match_all('~[^£$]~u', $s, $m)` will get you all of the chars. Use `'~[^£$]+~u'` to get whole chunks. – Wiktor Stribiżew Jan 28 '18 at 23:35
  • @Phil I'm trying to validate a string, and want the user to be able to put in anything apart from £ or $. So any text, numbers, symbols etc. all I want to do is block them from entering those two symbols absolutely anything else is fine. – Lewis Lebentz Jan 28 '18 at 23:38
  • 1
    @WiktorStribiżew I can't get that working on regex101.com is there something I need to change to test it there? – Lewis Lebentz Jan 28 '18 at 23:41
  • Right, so your actual question is _"how to detect specific symbols in a string?"_. The answer is simple; instead of trying to find all the other characters, simply look for those characters you don't want and fail if there are any found – Phil Jan 28 '18 at 23:41
  • 1
    Forget about regex101. You are not testing the Web site, are you? Always test regexps in the target environment. At any rate, if you need more relevant help, add the code to the question. – Wiktor Stribiżew Jan 28 '18 at 23:42
  • @Phil I guess so, yes. I thought the ^ excluded anything which is why I was trying to search with that. Should I remove that and modify the code to reverse how I'm doing things? – Lewis Lebentz Jan 28 '18 at 23:42
  • @WiktorStribiżew OP is probably confused by your `~` delimeters. FYI Lewis, you can use any character as the regex delimeter – Phil Jan 28 '18 at 23:43
  • @LewisLebentz yes. The affirmative test would be something like `/^[^£$]+$/` ie, an entire string, start-to-finish that does not contain any unwanted characters. Your test was only looking for one instance of any character not matching the character-class, an arbitrarily simple test to pass for just about any input string – Phil Jan 28 '18 at 23:45
  • @Phil thanks, I understand now! If this wasn't marked as a dupe I would've accepted your answer. So the first ^ tells it to start searching at the beginning of the string the [] tell it to search for the things inside, the ^ in the brackets searches for things other than the symbols inside, the + means keep searching unlimited times until $ which is the end. Is that correct? – Lewis Lebentz Jan 28 '18 at 23:51
  • 1
    More or less, yes. `[...]` is a [character class](https://www.regular-expressions.info/charclass.html). They represent a single character that must match the expression inside. The `+` symbol means _"one-or-more"_. I assumed you wouldn't want to pass an empty string as valid. If empty strings were valid, you would use `*`, ie _"zero-or-more"_ – Phil Jan 28 '18 at 23:54

0 Answers0