My regex pattern is failed [closed] - regex

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i need some help. My data;
{foreach $page_list as $page}
<li>{$page->name}</li>
{/foreach}
And my pattern;
~\{foreach\s\$(.+)\sas\s\$([^\{]+)\}([^\{]+)\{\/foreach\}~
But it's not working. What is wrong ?

The pattern with which you are trying to exclude the line between the two {foreach...} regions explicitly does not match '{' characters, but two are present in the string. Since that middle area's match is broken somewhere that doesn't get followed by a {/foreach}, the entire pattern fails to match.
That entire portion of the pattern should possibly be .* instead. Rely on backtracking to catch the closing {/foreach}. Of course, this will erroneously match an entire list as one thing, but regular expressions in general can't match quotes (without backreferences, which are technically non-regular and hurt performance), so you might need another parsing strategy entirely.

Related

how to find different endings to the same root word [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
trying to find both "itchy" and "itching"
I can find just "itch" but id like to be able to code to find whole words.
^itch - obviously only finds root word
The \w in Wictor’s answer matches any alphanumeric character, that is, 0-9a-zA-Z. If you don’t mind having words like “itch8y” in your output result, you can use \w. Otherwise use [a-zA-Z] to only match pure alphabetic letters.
\b zero-width matches word boundary. The caret ^ you used matches beginning of line instead.
If it’s possible the word you’re looking for has capitalized first letter, better search for \b[Ii]tch[a-zA-Z]*\b.

what is difference of (b*a)* and (ab*)*? Or they are same language? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
what is difference of (b*a)* and (b*a)*? Or they are same language?
The first one is a greedy matching (the asterisk quantifier after the scope) - matches as many characters as possible. The second one is a lazy matching (the asterisk and the question mark) of the same pattern. Lazy means to match as little as possible characters.
Read more here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions
Your greedy expression (the first one) will match the first 4 characters in input bbbac. The lazy algorithm will simply match nothing, because zero length matching is the minimum allowed. Nothing here means an epsilon, and infinite number of epsilons are assumed to exist around every character.
If you translate these patterns to an ABNF grammar you get this:
main = *(*"b" "a")
If you translate it to a language it is:

Matching up to special character [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to capture something until a , (comma).
Example payload1: AgentBrand: Internet Explorer,
Example Payload2: AgentBrand: OutlookPlug-in,
From the above payload I need to capture whatever coming after AgentBrand:
until the command (,).
I have Tried AgentBrand:\s+(\w+\s+\w+) .But this will be become a lengthy regex.
Thanks
The current regex that you are using - AgentBrand:\s+(\w+\s+\w+) will only catch cases where you have at least two words separated by a space and even in those cases it will only pick up the first two words (when there may be more).
A better regex to use would be - AgentBrand:\s*(.*?),
what this does is
AgentBrand: - looks for the string 'AgentBrand:'
\s* - matches zero or more space characters
(.*?) - captures any characters non-greedily [takes minimum matches to satisfy result]
, - looks for a comma at the end
Also as a note, the length of a regex is not always a good reason to avoid using one. The regex in your example is not that long and is quite simple and could be freely if it met all your requirements. Looking at the complexity of a regex is a better choice

Should I be escaping periods in my regular expressions? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So I have this regex:
^Something something something.$
Should I be preemptively escaping my periods with a backslash or would that be considered pedantic?
Am I wrong to assume that a period in this case is evaluated as a literal period?
No regex engine that I know of ever interprets an unescaped period as a literal period. If you want to match a period and nothing else, you must escape it. Do to otherwise is an error--it's just an error you'll usually get away with, because in most cases where you want to match a literal period, there's little possibility of matching the wrong thing.
There's nothing preemptive about it: always escape the period if you mean to match a period.

understanding the nitty and gritty of regular expressions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
need some talking through and explaining regular expressions as it seems obvious but then I will write one and it wont validate and I cant understand why.
I am using http://regexpal.com/ to test my expressions and am trying (ultimately) to validate a password. but to start I just want to simply match a string of words of 8 or more characters.
according to this http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ I should have most of what I need. This webpage tells me that \w matches a word character and then a + does 1 or more. this matches every individual expression I am testing (great). now I want to match only those that have 8 or more characters. so 'messi' should not validate but 'lollollol' should. so I then wrote this (\w+{8,}) expression but nothing was highlighted, aka nothing validated. I used () brackets to try and group everything together but it made no difference. can anyone see where my thinking is going wrong?
You must need to remove the following + from your regex. Since + does the job of repeating the previous token one or more times, likewise {8,} would repeat the previous token that is \w exactly 8 or more times. So you don't need to include the + in this (\w+{8,}) pattern.
(\w{8,})
Use capturing groups if necessary or otherwise go for matching.
\w{8,}