- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Hi Friends ,
It's my first blog and it may seem to be a small topic to write about but its surely an important concept in coding. I have seen quite a lot of posts (in community) and usage in Service Now .
I hope this blog helps you to understand about Regular expression
A regular expression is a special text/string for describing a search pattern. Use regular expressions to search through large numbers of text and binary files and then you can replace texts, rename files, copy files etc.
Meaning of different character/symbols:
- Twelve characters have special meanings in regular expressions: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {. These special characters are often called "metacharacters". Most of them will produce are errors when used alone.
- If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. So, if you want to match 2+3=5, the correct regex is 2\+3=5.
I would not mention all the concepts present in regular expression but will try to put all the important one's here.
- Anchors do not match any characters. They match a position. ^ matches at the start of the string, and $ matches at the end of the string. Example:^l matches only first l in lol.
- A character class [ ] specify a range of characters, We can use more than one range inside brackets. A caret after the opening square bracket negates the character class.
Example: [0-9a-z] matches only 0-9 digits and alphabets in (smaller case).
- Use curly braces { } to specify a repetition .Example: [A-Z]{2} two characters, each of which can be between A-Z.
Shorthand character classes:
- \d matches a single character that is a digit.
- \w matches a "word character" (alphanumeric characters plus underscore).
- \s matches a whitespace character.
- The dot matches a single character. Example: te.t matches text, test, tent etc.
- | is regular expression equivalent of "or".
To test if a particular regex matches (part of) a string, you can call the string match() or test() method.
Example: if(regexp.test(someValue)){}
Note: Regex engines are case sensitive by default.
I found below link very useful for learning regex. It lets you write the pattern and shows whether its correct or wrong.
Some community posts for similar topic which will help you understand more and solve your issue (if you have the same).
https://community.servicenow.com/thread/223016?start=0&tstart=0
https://community.servicenow.com/message/951790#951790
https://community.servicenow.com/message/930850#930850
https://community.servicenow.com/message/947083#947083
https://community.servicenow.com/thread/232272
https://community.servicenow.com/thread/232690
Hope you found this informative. Please encourage me to write more by hitting on like/share/bookmark (on top right corner)
- 11,010 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.