I have an expression like this:
5*3 + 2 +7 -sqrt(32312.323)
And i want to find all of the math operation in that expression. I've tried:
[A-Za-z\*\+\-\/]
Because of the "-" is next to the "sqrt" operation so it got combined as "-sqrt", i want them to be separated in this case ? How can i fix this ?
Thanks you in advance!
[*+\/-]+|[A-Za-z]+
This will works. According to #CertainPerformance The performance of that should be fine. The problem is always when several branches of alternation start with matching the same content (like a*|aa*, for example). There is no possibility your two branches will ever both match on the same character, so that is as good as regex gets.
Also, thanks for your answer #revo.
Related
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*")
I'm learning how to use the regular expressions for validating user inputs. I'm using Jquery to do it. I just want to know if it's possible to use an OR operator inside the expression.
I want to validate if the string has:
One letter first and then at least 6 digits
OR
First, at least 6 digits and then one letter
Examples :
X123456... OR 123456P
I use this /^[a-zA-Z]\d{6}/ for the first one but can I use something like this to take in consideration both conditions ?
/^[a-zA-Z]\d{6}/ | /^\d{6}/[a-zA-Z] ??
Thanks for the help
You nearly got it:
/^(?:[a-zA-Z]\d{6,}|\d{6,}[a-zA-Z])/
| is the alternation operator
Regular expressions have an alternative operator, which uses the same | symbol. You just need to put it inside the regexp:
/^([a-z]\d{6}|\d{6}[a-z])/i
Yes sure, you're pretty much there:
/^([a-zA-Z]\d{6}|\d{6}[a-zA-Z])/
I am a novice in regex and trying to understand it by solving small problems. So here I am with a problem which I couldn't solve (warning: it may be extremely silly). Your inputs will help me understand the concept.
I want to write a regex which will match all items in list1 but none of those from list2
list1
pit
spot
spate
slap two
respite
list2
pt
Pot
peat
part
I was thinking like "give me all the items that starts with p|s|r and endswith it|ot|e|o
So i wrote ^[p|s|r].*[it|ot|e|o]$ which eventually resulted in undesired result.
Thanks in advance for your inputs.
In notepad you can't do or operations (taken from Notepad++: A guide to using regular expressions and extended search mode and tested on my Notepad++ 5.9.3)
This would work in other "standard" regexes :-)
^[psr].*(it|ot|e|o)$
Try here. http://gskinner.com/RegExr/?2uudn
What were you doing was using the [] instead of the grouping (). It was equivalent to: [itoe|] (were the | was a "standard" character instead of or) and in general everything in an [] is in or :-) [ab] means a or b.
/(pit|spot|spate|slap two|respite)/.test('Pot')
This matches the words from list one, and none from list two
I feel like I must be missing something.
^(pit|spot|spate|slap two|respite)$
It depends entirely on how you categorise the differences between the lists:
/p[ioa\s]t/
I need to filter, 213.190.149.120 - 213.190.149.127 inclusive
Anyone know if there is a regular expression I can use to do this?
Thanks,
C
If you need a strict regular expression, don't forget that . matches any character, so
^213.190.149.(1(2[0-7]))$
will match "213d190c149a125" for example, which is not what you want.
On top of what, you're capturing each of the 3 digits, which is resource consuming for no apparent reason. A simple yet stricter regex would be closer to what #Marc suggested:
^213\.190\.149\.12[0-7]$
Don't know how Google Analytics expects the expression but this would be a valid regualar expression for your request:
213.190.149.12[0-7]
Okay.Found this link...
and it outputs...
^213\.190\.149\.(1(2[0-7]))$
Very handy for anyone else looking to do this.
Hey everyone, I'm trying to type a regular expression that follows the following format:
someone#somewhere.com or some.one#some.where.com
There are no special characters or numbers permitted for this criteria. I thought I had it down, but I'm a bit rusty with regular expressions and when I tested mine, it failed all across the boards. So far, my regular is expression is:
^[a-zA-Z]+/.?[a-zA-Z]*#[a-zA-Z]+/.?[a-zA-Z]*/.com$
If anyone could help me, it would greatly be appreciated, thanks.
your regex looks good. I think you need to change the / to \ in front of the . .
Additionally, if you don't want someone.#somewhere..com pass your regex, u should change your regex to
^[a-zA-Z]+(\.[a-zA-Z]+)?#[a-zA-Z]+(\.[a-zA-Z]+)?\.com$
(not completely sure about the brackets () though, but i think that should be working)
its a backslash to espace dots. Also put the the parenthesis around the . and what follows otherwise an email like abc.#cde..com would be valid.
^[a-zA-Z]+(\.[a-zA-Z]+)?#[a-zA-Z]+(\.[a-zA-Z]+)?\.com$
It looks mostly OK. Change your / to \ though...
For the second case, I would ensure that if you have a . in the middle, it must be followed by more letters:
^[a-zA-Z]+(\.[a-zA-Z]+)?#[a-zA-Z]+(\.[a-zA-Z]+)?\.com$