Ok, trying to change a string like so:
[topic]=2[board]=2
to return this instead:
[topic][board]=2
In the above situation, I'll be working with a string variable that equals [topic] and another string variable that equals [topic]=2[board]=2
But this needs to be done for all situations... More examples follow:
profile[area]=account[u]=1
should return this:
profile[area][u]=1
In the above situation, I'll be working with a string variable that equals profile and another string variable that equals profile[area]=account[u]=1
Another example:
moderate[area]=groups[sa]=requests
Should be this:
moderate[area][sa]=requests
In the above situation, I'll be working with a string variable that equals moderate and another string variable that equals moderate[area]=groups[sa]=requests
And another:
[board]=1
Should return:
[board]=1
In the above situation, I'll be working with a string variable that equals [board] and another string variable that equals [board]=1
Basically, what it needs to be able to do, is to get rid of the text in between ONLY the text that are between the brackets of only the first and second brackets (if the second bracket exists only). It should not effect any third, fourth, 5th brackets. Only the first and second brackets.
Can someone please give me a hand with this?
Thanks :)
Here's a regular expression that works:
(?<=\])\=[^\[\r\n]*(?=\[)
http://regexr.com?2vn71
That \r\n might need to be changed to a \Z in PHP, since you won't be dealing with line breaks, but rather with the end of a string.
So, something like:
preg_replace("/(?<=\])\=[^\[\Z]*(?=\[)/", "", $target);
Edit:
Here's a breakdown of what's happening here:
(?<-\]) Make sure there's a [ character before the matched expression
\= Match a = character
[^\[\Z]* Match all characters until you find a [ or the end of the string (\Z)
(?=\[) Make sure there's a [ after the matched expression
Another regex, designed to be used with preg_replace()
preg_replace('/^([a-z]+)?(\[[a-z]+\])(.*)(\[(.*))$/Uis', '$1$2$4', $yourData);
Related
So i will provide this simple example of json string covering most of my actual string cases:
"time":1430702635,\"id\":\"45.33\",\"state\":2,"stamp":14.30702635,
And i'm trying to do a preg replace to the numbers from the string, to enclose them in quotes, except the numbers which index is already quoated, like in my string - '\state\':2
My regex so far is
preg_replace('/(?!(\\\"))(\:)([0-9\.]+)(\,)/', '$2"$3"$4',$string);
The rezulting string i'm tring to obtain in this case is having the "\state\" value unquoted, skipped by the regex, because it contains the \" ahead of :digit,
"time":"1430702635",\"id\":\"45.33\",\"state\":2,"stamp":"14.30702635",
Why is the '\state\' number replaced also ?
Tried on https://regex101.com/r/xI1zI4/1 also ..
New edit:
So from what I tried,
(?!\\")
is not working !!
If I'm allowed, I will leave this unanswered in case someone else does know why.
My solution was to use this regex, instead of NOT, I went for yes ..
$string2 = preg_replace('/(\w":)([0-9\.]+)(,)/', '$1"$2"$3',$string);
Thank you.
(?!\\") is a negative lookahead, which generally isn't useful at the very beginning of a regular expression. In your particular regex, it has no effect at all: the expression (?!(\\\"))(\:) means "empty string not followed by slash-quote, then a colon" which is equivalent to just trying to match a colon by itself.
I think what you were trying to accomplish is a negative lookbehind, which has a slightly different syntax in PCRE: (?<!\\"). Making this change seems to match what you want: https://regex101.com/r/xI1zI4/2
I have created a Regular Expression (using php) below; which must match ALL terms within the given string that contains only a-z0-9, ., _ and -.
My expression is: '~(?:\(|\s{0,},\s{0,})([a-z0-9._-]+)(?:\s{0,},\s{0,}|\))$~i'.
My target string is: ('word', word.2, a_word, another-word).
Expected terms in the results are: word.2, a_word, another-word.
I am currently getting: another-word.
My Goal
I am detecting a MySQL function from my target string, this works fine. I then want all of the fields from within that target string. It's for my own ORM.
I suppose there could be a situation where by further parenthesis are included inside this expression.
From what I can tell, you have a list of comma-separated terms and wish to find only the ones which satisfy [a-z0-9._\-]+. If so, this should be correct (it returns the correct results for your example at least):
'~(?<=[,(])\\s*([a-z0-9._-]+)\\s*(?=[,)])~i'
The main issues were:
$ at the end, which was anchoring the query to the end of the string
When matching all you continue from the end of the previous match - this means that if you match a comma/close parenthesis at the end of one match it's not there at match at the beginning of the next one. I've solved this with a lookbehind ((?<=...) and a lookahead ((?=...)
Your backslashes need to be double escaped since the first one may be stripped by PHP when parsing the string.
EDIT: Since you said in a comment that some of the terms may be strings that contain commas you will first want to run your input through this:
$input = preg_replace('~(\'([^\']+|(?<=\\\\)\')+\'|"([^"]+|(?<=\\\\)")+")~', '"STRING"', $input);
which should replace all strings with '"STRING"', which will work fine for matching the other regex.
Maybe using of regex is overkill. In this kind of text you can just remove parenthesis and explode string by comma.
im finding searchwords from google request urls.
im using
preg_match("/[q=](.*?)[&]/", $requesturl, $match);
but it fails when the 'q' parameter is the last parameter of the string.
so i need to fetch everything that comes after 'q=', but the match must stop IF it finds '&'
how to do that?
EDIT:
I eventually landed on this for matching google request url:
/[?&]q=([^&]+)/
Because sometimes they have a param that ends with q. like 'aq=0'
You need /q=([^&]+)/. The trick is to match everything except & in the query.
To build on your query, this is a slightly modified version that will (almost) do the trick, and it's the closest to what you have there: /q=(.*?)(&|$)/. It puts the q= out of the brackets, because inside the brackets it will match either of them, not both together, and at the end you need to match either & or the end of the string ($). There are, though, a few problems with this:
sometimes you will have an extra & at the end of the match; you don't need it. To solve this problem you can use a lookahead query: (?=&|$)
it introduces an extra group at the end (not necessarily bad, but can be avoided) -- actually, this is fixed by 1.
So, if you want a slightly longer query to expand what you have there, here it is: /q=(.*?)(?=&|$)/
Try this:
preg_match("/q=([^&]+)/", $requesturl, $match);
A little explaining:
[q=] will search for either q or =, but not one after another.
[&] is not needed as there is only one character. & is fine.
the ? operator in regex tells it to match 0 or 1 occurrences of the ** preceding** character.
[^&] will tell it to match any character except for &. Which means you'll get all the query string until it hits &.
I need my following code to work.
I am trying to use a PHP Variable and also add a [charlist] Wildcard statement to it
Could some one please help figure out why it wont work.
Basically it works if i remove the [charlist] Wildcard but I need it to find all the letters which are in the PHP Variable
my code is as followed
LIKE ''[' $searchWord ']%''
To use a character class, you need to use the REGEXP operator.
Additionally, after a character class, you need to indicate a repetition operator. % matches any string (and is only for LIKE), but if you want to apply it so that it will match any series of letters contained within your character class, you need to do:
$query = '`column` REGEXP "[' . $searchWord . ']+"';
Alternatively, use a * to match 0 or more. (+ is 1 or more)
If $searchWord is an array, try it by calling implode first on it:
$listOfCharacters = str_split($searchWord, 1);
$implodedString = implode(',', $listOfCharacters;
The imploded string is a comma seperated string now instead of an array. PHP doesn't convert arrays to string by itself.
Then you can probably use it like:
LIKE ''[' $implodedString ']%''
Although I'm suspicious about using it without string concatonation here. Do we miss some piece of code here?
I need a very specific function in PHP. Basically, I have two strings as arguments, one of which is a pattern that contains wildcards of variable length (*), and one of which is a string that matches that pattern. I need to get an array of the strings from the latter string that fill in the wildcards in the pattern.
For example:
Argument 1: "This is * string that I *"
Argument 2: "This is my awesome string that I created myself"
Return: array("my awesome","created myself")
What's the cleanest way to do this? Bear in mind that these are not always strings of english words as in the example, they could be any random characters.
You could just replace the wildcards with a regex equivalent, and run it through preg_match.
Because it really does sound like homework, I won't give any specific code either, but I'd make good use of preg_replace (to replace the wildcards with regex equivalents) and preg_match (to build the array of matching substrings).
Sounds like homework. Here is a walk-through without any actual code:
Tokenize the pattern into strings and wildcards. Iterate over the tokens; each time the target string starts with a regular (string, non-wildcard) token, trim that string off. Each time you encounter a wild card token, find the index of the next token in the string and trim off up to that index. Store that. If at any time there is no match, return false. If you encounter the end of the string before the pattern is complete, return false. If the final token is a wildcard, save the remainder of the string.