preg_match regex syntax - php

Tried different regex generators with no luck.
I have this string that i put in preg_match:
$search_string = "/^:([A-Za-z0-9_\-]+)[#!~a-zA-Z0-9#\.\-]+\s*([A-Z]+)\s*[:]*([\#a-zA-Z0-9\-]+)*\s*[:]*([!\#\-\.A-Za-z0-9 ]+)*/";
It's basically for usernames. Sadly, when username has underscore in it. For example iam_coolguy wouldn't work.
How to add underscore to this search string?
I can't seem to figure out how regex works.
It's not a duplicate, scrolled past all preg_match threads.
/[a-z]/i seems easy and understandable for me, but my string is too advanced for my knowledge.
Thanks.

If you are just looking to grab somthing between //'s
I would just use this regex \/(.*)\/
but as the others have said, you havent given any limitations on what the username can and can't have in it.
If you need more, say something and I will adjust my answer.

Related

What's the best approach to find words from a set of words in a string?

I must detect the presence of some words (even polyrematic, like in "bag of words") in a user-submitted string.
I need to find the exact word, not part of it, so the strstr/strpos/stripos family is not an option for me.
My current approach (PHP/PCRE regex) is the following:
\b(first word|second word|many other words)\b
Is there any other better approach? Am I missing something important?
Words are about 1500.
Any help is appreciated
A regular expression the way you're demonstrating will work. It may be challenging to maintain if the list of words grows long or changes.
The method you're using will work in the event that you need to look for phrases with spaces and the list doesn't grow much.
If there are no spaces in the words you're looking for, you could split the input string on space characters (\s+, see https://www.php.net/manual/en/function.preg-split.php ), then check to see if any of those words are in a Set (https://www.php.net/manual/en/class.ds-set.php) made up of the words you're looking for. This will be a bit more code, but less regex maintenance, so ymmv based on your application.
If the set has spaces, consider instead using Trie. Wiktor Stribiżew suggests: https://github.com/sters/php-regexp-trie

Php highlight search terms using preg_replace - character encoding issues

Hy guys, I need to highlight some searched words, in the result, and i was thinking to use pre_replace, and it works just fine, until i use accented characters.
So this is my code:
preg_replace("/(?<!\[)(\b{$search}\b)(?!\])/i", $replace, $string);
And if I'm looking for the word "mokus", it finds it, but leaves out "mókus",
The same thing happens the other way around.
And ideas? Thanks in advance.
You might want to research the term Accent Folding.
Here's a good article to understand the problem, the proposed solutions are in Javascript but you can translate the logic to PHP

Specific regex pattern to stop advertisement

I'm using the php function preg_match() to check for a specific pattern.
I've gone through about 50 websites so far, and still haven't figured out how to do this specific pattern.
If there is the word "dot" or "d0t" after anything and before anything that contains "com" or "org", it would catch it.
I'm making something that filters out advertisement, it separately filters out anything except for letters, numbers, and underscores; that has to stay separate. It has it's own purpose, and it's own output.
If you can help me figure out how to do this, or link me to anything that I may have missed after 2 hours of googling, I would so greatly appreciate it.
Thanks.
Question is not very clear but you can try this regex for preg_match:
'~d[0o]t.*?(?:com|org)~i'
This matches word "dot" or "d0t" before anything that contains "com" or "org"

Basic Regular Expression for

For some reason I always get stuck making anything past extremely basic regular expressions.
I'm trying to make a regular expression that kind of looks like a URL. I only want basic checking.
I would like it to match the following patterns where X is "something".
X://X.X
X://X.X... etc.
X.X
X.X... etc
If the string contains one of these patterns, it is sufficient checking for me. This way a url like www.example.com:8888 will still match. I have tried many different REGEX combinations with preg_match and cannot seem to get any to behave the way I want it to. I have consulted many other related REGEX questions on SO but my readings have not helped me.
Any help? I will be happy to provide more information if you would like but I don't know what else you would need.
It takes practice but here is one that I made using a regex tester (http://www.regextester.com/) to check my pattern:
^.+(:\/\/|\.)([a-zA-Z0-9]+\.)+.+
My approach is to slowly build my pattern from the beginning and add on one piece at a time. This cheatsheet is extremely helpful for remembering http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ what everything is.
Basically the pattern starts at the beginning of the string and checks for any characters followed by either :// or . then checks for groupings of letters and numbers followed by a . ending with any number of characters.
The pattern could probably be improved with groupings to not pass on invalid characters. But this one was quick and dirty. You could replace the first and last . with the characters that would be valid.
UPDATE
Per the comments here is an updated pattern:
^.+?(:\/\/|\.)?([a-zA-Z0-9]+?\.)+.+
/^(.+:\/\/)?[^.]+\.[^.\/]+([.\/][^.\/]+)*$/

search for a sentence in a paragraph

How can I search a paragraph for one or more sentences using PHP?
Yes. strpos will tell you whether a string exists within a string.
Have a look over here, maybe here and here.
Well, you'll probably want to use "preg_match()" if possible (requires knowledge of regular expressions though). "strstr()" works too if you know exactly what you want to find.

Categories