Regular Expression to recognise truncated forms of search string? - regex

I'm trying to formulate a regular expression which will recognise the search term truncated by any number of characters from the right.
For example, if the search term is "pickle", the regex should recognise "pi", "pick" but not "pickaxe".
Initially I came up with the following:
p(i(c(k(l(e)?)?)?)?)?
That works perfectly, but seems a crude way of doing it. Is there a better way of doing this? I had a look around for something similar to what I want, but I'm not entirely sure what to search for.

Due to the way regex works, yes, that's basically the most concise form.

Related

How to exclude the last part of a variable string using regex

I am currently making a bunch of landing pages that use similar URL structure, but each URL varies in number of words.
So it's something like:
http://landingpage.xyz/page-number-five
http://landingpage.xyz/page-number-fifty-four
http://landingpage.xyz/page-for-a-different-topic
and for the sent page I just postfix -sent like this. The reason I am not adding it as /sent is because the platform I am using handles URLs this way.
http://landingpage.xyz/page-number-five-sent
http://landingpage.xyz/page-number-fifty-four-sent
http://landingpage.xyz/page-for-a-different-topic-sent
Now I found it easy to make a regular expression that identifies all the sent pages which is let's say:
\/([a-z0-9\-]*)-sent
The thing is that I am not sure how to identify the ones that are not sent. I tried using a similar regular expression using something like this, but it's not working as expected:
\/([a-z0-9\-]*)(?!-sent)
What's the best way to design the regex for this? Or I am approaching it in the wrong way?
A lookahead should be considered where there are some characters left to match. So one at the end of regex doesn't look for anything. As long as I'm not sure whether or not your environment supports lookbehinds, this should be a workaround:
\/(?!.*-sent\b)([a-z0-9\-]*)

regular expression for string with dot del

I am looking for a regular expression for the following string:
SQL Err 100 on \TEST1.$PROD01.TEST.XYZ562
I want to search for anything with TEST*.$PROD*.TEST.XYZ*.
Can anyone help with the regular expression?
Basic effort was required, you could just tried it yourself and, I believe, find an answer on your own! For future needs, try site regex101.com, it helps to work with regulars.
So, you need to find a string containing TEST, then something, then . then $PROD value, again anything, and .TEST.XYZ, and finally anything, where $PROD is, as I understand, a variable without special characters (as [](){}.^\).
So, you get that:
TEST[^.]*\.<$PROD value should go here>[^.]*\.TEST\.XYZ
That should be enough. You will need to supstitude variable $PROD on your own, but I don't know your language, so I can't help with that part. Maybe something like this:
"TEST[^.]*\." + $PROD + "[^.]*\.TEST\.XYZ"

Using a regular expression to insert text in a match

Regular Expressions are incredible. I'm in my regex infancy so help solving the following would be greatly appreciated.
I have to search through a string to match for a P character that's not surrounded by operators, power or negative signs. I then have to insert a multiplication sign. Case examples are:
33+16*55P would become 33+16*55*P
2P would become 2*P
P( 33*sin(45) ) would become P*(33*sin(45))
I have written some regex that I think handles this although I don't know how using regex I can insert a character:
The reg is I've written is:
[^\^\+\-\/\*]?P+[^\^\+\-\/\*]
The language where the RegEx will be used is ActionScript 3.
A live example of the regex can be seen at:
http://www.regexr.com/39pkv
I would be massively grateful if someone could show me how I insert a multiplication sign in middle of the match ie P2, becomes P*2, 22.5P becomes 22.5P
ActionScript 3 has search, match and replace functions that all utilise regular expressions. I'm unsure how I'd use string.replace( expression, replaceText ) in this context.
Many thanks in advance
Welcome to the wonder (and inevitable frustration that will lead to tearing your hair out) that is regular expressions. You should probably read over the documentation on using regular expressions in ActionScript, as well as this similar question.
You'll need to combine RegExp.test() with the String.replace() function. I don't know ActionScript, so I don't know if it will work as is, but based on the documentation linked above, the below should be a good start for testing and getting an idea of what the form of your solution might look like. I think #Vall3y is right. To get the replace right, you'd want to first check for anything leading up to a P, then for anything after a P. So two functions is probably easier to get right without getting too fancy with the Regex:
private function multiplyBeforeP(str:String):String {
var pattern:RegExp = new RegExp("([^\^\+\-\/\*]?)P", "i");
return str.replace(pattern, "$1*P");
}
private function multiplyAfterP(str:String):String {
var pattern:RegExp = new RegExp("P([^\^\+\-\/\*])", "i");
return str.replace(pattern, "P*$1");
}
Regex is used to find patterns in strings. It cannot be used to manipulate them. You will need to use action script for that.
Many programming languages have a string.replace method that accepts a regex pattern. Since you have two cases (inserting after and before the P), a simple solution would be to split your regex into two ([^\^\+\-\/\*]?P+ and P+[^\^\+\-\/\*] for example, this might need adjustment), and switch each pattern with the matching string ("*P" and "P*")

Select start and end of a RegEx

I'm having trouble naming this question, and it feels like it's something I should have found myself, but I'm too dumb it seems. RegEx is still incredibly complicated to me, so please don't be too harsh to me.
Basically, I have a huge list of text of which I need to extract certain word sections. I know the mask around the word, but I obviously only need the word itself. Let me try to give you a simple example:
<b>Name1</b>
<i>Name2</i>
<u>Name3</u>
I can clearly see the things I want are all surrounded by <> tags. My approach was always to find the entire string and then simply do a plain replace to get rid of these extra characters.
<\w>{1}\w+<\/\w>{1}
string.replace("<b>","");
string.replace("</b>","");
... and so on.
However, something just feels wrong about it. Like, incredibly wrong. Can't I just directly say in my RegEx search what exactly I'm looking for? Like:
<\w>{1}START\w+END<\/\w>{1}
Does something like this exist?
(This is a general question, not a specific problem, so please don't provide alternate workarounds or something. I've had this problem many, many times already, and I'm fed up with solving it with this hackish way.)
A regex like (?!<\w>)\w+(?=<\/\w>) might be what you are looking for. See example here regextester
How about <[^>]+>([^<]+)<\/[^>]+>? It'll match the whole "tag", but it'll only capture what's between the tags...

Need to create a gmail like search syntax; maybe using regular expressions?

I need to enhance the search functionality on a page listing user accounts. Rather than have multiple search boxes for each possible field, or a drop down menu where the user can only search against one field, I'd like a single search box and to use a gmail like syntax. That's the best way I can describe it, and what I mean by a gmail like search syntax is being able to type the following into the input box:
username:bbaggins type:admin "made up plc"
When the form is submitted, the search string should be split into it's separate parts, which will allow me to construct a SQL query. So for example, type:admin would form part of the WHERE clause so that it would find any record where the field type is equal to admin and the same for username. The text in quotes may be a free text search, but I'm not sure on that yet.
I'm thinking that a regular expression or two would be the best way to do this, but that's something I'm really not good at. Can anyone help to construct a regular expression which could be used for this purpose? I've searched around for some pointers but either I don't know what to search for or it's not out there as I couldn't find anything obvious. Maybe if I understood regular expressions better it would be easier :-)
Cheers,
Adam
No, you would not use regular expressions for this. Just split the string on spaces in whatever language you're using.
You don't necessarily have to use a regex. Regexes are powerful, but in many cases also slow. Regex also does not handle nested parameters very well. It would be easier for you to write a script that uses string manipulation to split the string and extract the keywords and the field names.
If you want to experiment with Regex, try the online REGex tester. Find a tutorial and play around, it's fun, and you should quickly be able to produce useful regexes that find any words before or after a : character, or any sentences between " quotation marks.
thanks for the answers...I did start doing it without regex and just wondered if a regex would be simpler. Sounds like it wouldn't though, so I'll go back to the way I was doing it and test it again.
Good old Mr Bilbo is my go to guy for any naming needs :-)
Cheers,
Adam