help with regular expression - php

is it possible to get a regular expression that do:
find first occurence of some word in a string
return a substring of a nr of letters before and after the occurence
but if it encounters a . (dot) before the nr of letters before and after occurence, it will just return the sub string before/after the dot.
return whole words
example:
"Anyone who knows how to do this. Create a program that inputs a regular expression and outputs strings that satisfy that regular expression. And bla bla"
if the keyword is 'program' and we put nr of letters to 20 it will return 20 letters before and after 'program'. But since it encounters a dot before it gets to 20 letters it will stop there.
"Create a program that inputs a regular..."
Is this possible with regexp? what php function do i have to use? is there any finnished script for this? I guess its a quite basic need when showing search results. Someone already got the function to share?

Here's Dav's regular expression in php:
<?php
$str = "Anyone who knows how to do this. Create a program that inputs a regular expression and outputs strings that satisfy that regular expression. And bla bla";
$key = "program";
$lim = 20;
$reg = "/([^.]{0,{$lim}})({$key})([^.]{0,{$lim}})/"; // /[^.]{0,20}program[^.]{0,20}/
$res = preg_match($reg, $str, $matches);
echo $matches[0];
print_r($matches); // $matches[1] is the pre-text, and $matches[3] is the post-text
The trickiest requirement is #4: "return whole words". One way you could handle this while still making use of the above regular expression is pull more text before and after than you really want (say 40 chars). Then you could preg_split the before and after text on whitespace, which would give you two arrays of words. Run the arrays through a function that gives you back a subset of the array where the total length of all the words is less than your limit of 20...

[^.]{0,MAXCHARS}wordtofind[^.]{0,MAXCHARS}
Replace MAXCHARS with the number corresponding to the maximum number of characters you want on each side.
The [^.] pattern matches any character that's not a period, and the {0,MAXCHARS} qualifier matches anywhere from 0 to MAXCHARS of those characters.

Related

PHP Regex - how to remove string after "Newline * regards"

I've got strings like the following:
Hi X
Blah
Kind regards
ABC
And
Hi X
Blah
Regards
CBA
So the key is the newline and the word "regards" (case insensitive). I'd like to use PHP to get the part of the string before the line that contains "regards". E.g. for these examples, the result should just be:
Hi X
Blah
I've tried the below but it doesn't work as intended in some cases (E.g. if "Kind" appears multiple times in the string). Thanks in advance!
$matches = array();
if (preg_match("/\n(.*?)regards/i", $message, $matches) == 1) {
$stop_at = $matches[1];
$split = explode($stop_at,$message);
$message = $split[0];
}
What you're really after is a regex that handles multi-line strings. For this, you can use the m flag (PCRE_MULTILINE).
I would use preg_split() to split the string on your token, for example
$found = trim(preg_split('/^.*regards$/im', $message, 2)[0]);
Demo ~ https://3v4l.org/idMcP
Some notes:
I've used trim() to remove the empty line after "Blah" (your examples exclude it)
I've set a limit of 2 on preg_split(). This is redundant given you're only retrieving the first split but in my head, it means PHP does less work (realities may vary).
This might fail if a line ends in a word ending in "regards" but not necessarily the word "regards", for example this word I just made up "goregards" (it's like a shin guard but for viscera).
You can use the regular expression
(?si).+\b(?=\n+[\w ]*regards)
It will match everything up to a word boundary, then lookahead for newline(s) followed by a line which has regards on it (possibly preceeded by a combination of word characters or spaces).
$str = "Hi X
Blah
Kind regards
ABC";
preg_match('/(?si).+\b(?=\n\s*[\w ]*regards)/', $str, $match);

Move multiple letters in string using regex

Using a regular expression I want to move two letters in a string.
W28
L36
W29-L32
Should be changed to:
28W
36L
29W-32L
The numbers vary between 25 and 44. The letters that need to be moved are always "W" and/or "L" and the "W" is always first when they both exist in the string.
I need to do this with a single regular expression using PHP. Any ideas would be awesome!
EDIT:
I'm new to regular expressions and tried a lot of things without success. The closest I came was using "/\b(W34)\b/" for each possibility. I also found something about using variables in the replace function but had no luck using these.
Your regex \b(W34)\b matches exactly W34 as a whole word. You need a character class to match W or L, and some alternatives to match the numeric range, and use the most of capturing groups.
You can use the following regex replacement:
$re = '/\b([WL])(2[5-9]|3[0-9]|4[0-4])\b/';
$str = "W28\nL36\nW29-L32";
$result = preg_replace($re, "$2$1", $str);
echo $result;
See IDEONE demo
Here, ([WL]) matches and captures either W or L into group 1, and (2[5-9]|3[0-9]|4[0-4]) matches integer numbers from 25 till 44 and captures into group 2. Backreferences are used to reverse the order of the groups in the replacement string.
And here is a regex demo in case you want to adjust it later.

Match only the first set of numbers in a string

I need to retrieve the first set of numbers from a string, but I'm not sure how.
I have the following, which I was expecting to pick each set of numbers so that I could then pick the first key from the $matches array, but it literally matches only the first number.
In this example I'd be looking for '123'. Can someone please let me know how to do this with RegEx (or a better way if RegEx is not best for the job). Thanks.
$e = 'abc 123,456,def, 789-ab-552'; // Just a random example
$pattern = "/[0-9]/";
preg_match($pattern, $e, $matches);
You must add a quantifier:
$pattern = "/[0-9]+/";
+ means one or more
You can find an ajax regex tester for php here and more informations about regular expressions here.

Remove all before specific symbol in string PHP

For example i have string one two three four five and I want remove all characters before two and after four , I know that is function preg_replace() but I don't know how to write this expression, I not know what does mean for example '/([a-z])([A-Z])/' please say what is name of this expression in $pattern and what they mean
In case you're looking for preg_replace based solution then here it is:
$str = 'one two three four five';
var_dump ( preg_replace('#^.*?(two.*?four).*$#i', '$1', $str) );
Expanation: RegEx used in preg_replace first matches text upto your starting text two then matches upto your end text four and finally replaces it with matched string thus discarding all text before two and all text after four. Please note .*? makes your matching non-greedy. Read more about regex here: http://www.regular-expressions.info/
OUTPUT
string(14) "two three four"
preg_replace is a function which takes in regular expressions to do replacements.
You can and should learn about these, as they are very powerful, but they aren't essential for your problem.
You can use strpos and substr functions
substr takes a string that you want to shorten, a start location and a number of characters and returns the shortened string.
strpos takes a string that you want to search, and a string that you want to search for and returns the location of the second string in the first.
So you can use them as such:
$text = "one two three four five";
$locationOfTwo = strpos($text, "two"); //The location in the string of the substring "two" (in this case it's 4)
$locationOfFour =strpos($text, "four") + strlen("four"); //Added the length of the word to get to the end of it (In this case it will set the variable to 18).
$subString = subsstr($text, locationOfTwo, (locationOfFour-locationOfTwo));

How to get the word which is in first quotation marks?

Let's say I have the this text (not to be treated as PHP code):
$this->validation->set('username','username','trim');
$this->validation->set('password','password','trim');
$this->validation->set('password2','password2','trim');
$this->validation->set('name','name','trim');
$this->validation->set('surname','surname','trim');
I want to get the list of first words after set( which is in quotation marks in every line, so the output of previous input must be like this:
username
password
password2
name
surname
I think, it's possible with regular expressions. My question is how can I get the list of the words which is in first quotation marks with PHP?
Lets say the variable $text holds the data from your question.
Let's analyse the regular expression /set\('(.*?)'/:
/ is the delimiter.
set\(' and ' are the strings set(' and ', respectively.
.*? is the least amount of (arbitrary) characters between the two aforementioned strings.1
As a result, this regular expression matches:
$this->validation->set('username','username','trim');
To store all the strings you need in the array $matches[1], we can use the function preg_match_all.
It suffices to call preg_match_all("/set\('(.*?)'/", $text, $matches).
1 See also: Regex Tutorial - Repetition with Star and Plus - Laziness Instead of Greediness
Example code:
$text = <<<EOF
\$this->validation->set('username','username','trim');
\$this->validation->set('password','password','trim');
\$this->validation->set('password2','password2','trim');
\$this->validation->set('name','name','trim');
\$this->validation->set('surname','surname','trim');
EOF;
preg_match_all("/set\('(.*?)'/", $text, $matches);
print_r($matches[1]);
$arr = explode("'","this->validation->set('surname','surname','trim')");
print_r($arr);
not sure why you would want to do something like that, but the above should work

Categories