Difference between revisions of "Regular Expression"
From GLMWiki
(Created page with "RegEx can be tricky! Hopefully this will provide some help. This is an example of perl-regex format which is used in Netbeans and Atom <pre> a-c(\d[{2-2}]\d{1}?[{0-9}]\d) </p...") |
|||
(One intermediate revision by the same user not shown) | |||
Line 6: | Line 6: | ||
</pre> | </pre> | ||
− | This will match a-c213 as well as a-c2199 | + | This will match a-c213 as well as a-c2199. It will not match b2199 nor a-c229. Can you figure out why? |
+ | |||
+ | The ? makes the following statement optional. | ||
+ | |||
+ | \d is a digit | ||
+ | |||
+ | [{x-y}] indicates a range from x to y. This can be alphanumeric. | ||
+ | |||
+ | {1} indicates that just that character is accepted. | ||
+ | |||
+ | Everything outside the parentheses is literal. So "a-c" matches that string, literally. | ||
+ | |||
+ | If you perform a search/replace, you can use $1 to grab the first match, $2 for the second, etc. | ||
+ | <pre> | ||
+ | e.g. | ||
+ | input: What is a word | ||
+ | search: What is a(.*) | ||
+ | replace: This is not a$1 | ||
+ | result: This is not a word | ||
+ | </pre> | ||
+ | |||
+ | The (.*) above is a wildcard, it'll match any number of anythings, like the space after "a"! |
Latest revision as of 17:13, 25 August 2016
RegEx can be tricky! Hopefully this will provide some help.
This is an example of perl-regex format which is used in Netbeans and Atom
a-c(\d[{2-2}]\d{1}?[{0-9}]\d)
This will match a-c213 as well as a-c2199. It will not match b2199 nor a-c229. Can you figure out why?
The ? makes the following statement optional.
\d is a digit
[{x-y}] indicates a range from x to y. This can be alphanumeric.
{1} indicates that just that character is accepted.
Everything outside the parentheses is literal. So "a-c" matches that string, literally.
If you perform a search/replace, you can use $1 to grab the first match, $2 for the second, etc.
e.g. input: What is a word search: What is a(.*) replace: This is not a$1 result: This is not a word
The (.*) above is a wildcard, it'll match any number of anythings, like the space after "a"!