_ _
Creating Regular Expressions
...
For example, suppose you want to find the value from the following snippet:
No Format |
---|
input type="hidden" name="secret" value="CAFEBABE.12345(3)"
|
...
If not, examine the expression for any meta-characters (characters that have a special meaning in regexes).In this case, the only special characters are the "." and the parentheses "(" and ")".These need to be escaped, by being prefixed with "\". We now have:
No Format |
---|
input type="hidden" name="secret" value="CAFEBABE\.12345\(3\)"
|
...
So assume you want to match just
No Format |
---|
CAFEBABE.12345
|
Your regular expression then becomes:
No Format |
---|
input type="hidden" name="secret" value="(CAFEBABE\.12345)\(3\)"
|
...
A digit is easy, that's "\d
", so a number is "\d+
", where the "+
" means one or more of the previous item.
A hex character can be represented by "\ Wiki Markup [0-9A-Za-z
\]
". The enclosing "\[
\]
" create a _character class_ which matches one of the specified characters. The "-
" here means a range.
So putting that together, we get:
No Format |
---|
input type="hidden" name="secret" value="([0-9A-Za-z]+\.\d+)\(d\)" |
Now suppose we wanted to match the whole of the value. We could move the closing capture parenthesis to the end of the value. This would be suitable if there were other values with different patterns that we did not want to match.
However, if we just wanted to capture the quoted value, then we could use:
No Format |
---|
input type="hidden" name="secret" value="([^"]+)"
|
...
The character class in this case is _\[^"
\]
_ which means any character except double-quote. The "+
" suffix means we want as many as there are in succession. This will take us up to the end of the value.
Hints and tips
If the expression matches more than once in the source, you have a choice:
...