- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 07-07-2021 11:16 AM
Most flexible Regular Expression
After having written a few articles about Regular expressions (Field validation and Common Regular Expressions) , I get a lot of questions from Community members and colleagues how to find a value between two strings, or a string value and an enter or space, or a value that is between brackets.
Examples
Given information leaded by "Configuration item - " and followed by a space or enter:
User name - Willem
Configuration item - CI-07072021
What we want: CI-07072021
Given information:
Given information between brackets (can be [ ] or { } or any other character):
Configuration [CI-07072021]
What we want: CI-07072021
The solution
This can be done fairly simple by making use of capturing groups. Meaning we can capture the value we are looking for in a capture group and exclude all other values in a non-capturing group:
(abc) | Capture group |
(?:abc) | Non-capture group |
var regex = /(?:A)(.*)(?:B)/g;
When using for example Regular expression .exec, that will return an Array of the results. The first value in this Array is the complete matching string, the second value is the first Capture group.
The script:
We want the value beween A:"Configuration item - " and B: "\s" (white-space).
(replace inText with the variable/input you want to retrieve the value from)
var inText = "User name - Willem\
\
Configuration item - CI-07072021 ";
var regex = /(?:Configuration item - )(.*)(?:\s)/g;
inText = regex.exec(inText);
gs.print(inText);
The result:
So it returns:
- Configuration item - CI-07072021
- CI-07072021
As this is an Array and we are only interested in the CI-07072021 you can print inText[1] Like so:
var inText = "User name - Willem\
\
Configuration item - CI-07072021 ";
var regex = /(?:Configuration item - )(.*)(?:\s)/g;
inText = regex.exec(inText);
gs.print(inText[1]);
Result:
Explanation
Between A and B:
var regex = /(?:A)(.*)(?:B)/g;
The script will return all characters (.*) between A and B:
var inText = "A This is a lot of text and I want to find it B";
var regex = /(?:A)(.*)(?:B)/g;
inText = regex.exec(inText);
gs.print(inText[1]);
Result (including spaces):
This is a lot of text and I want to find it
An example you commonly see is a value between brackets:
(Please be aware that for special characters you might have to Escape by prefixing with a \ . So \[ and \] if you want a bracket)
var regex = /(?:\[)(.*)(?:\])/g;
Input:
var inText = "Configuration [CI-07072021]";
var regex = /(?:\[)(.*)(?:\])/g;
inText = regex.exec(inText);
gs.print(inText[1]);
Result:
Let me know if there are any questions or things that can be more clear. If you find the article helpful, feel free to Like and share it! 😄
- 2,780 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Wow. Great timing, Willem. I will be able to use this for a requirement that just came up today.
Thanks!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Jerome,
Always great to hear it is useful! Thank you!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
Thank you for your article.
I use this but That does not work for me
I want to have the text 'Paie/GTA' for this string :
'
<p>Société: HEXAGONE</p>
<p>Thème: Paie/GTA</p>
<p>ADP PAIE: faux</p>
<p>ADP GTA: VRAI</p>
'
So i do this fix script :
var inText = hrCase.rich_description;
var regex = /(?:Thème: )(.*)(?:<p)/g;
gs.info('regex', regex.toString());
inText = regex.exec(inText);
gs.info("inText", inText[1]);
But that does not work
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Use this, it works.
var regex = /(?:Thème: )(.*)(?:<)/g;
var inText = regex.exec(inText);
gs.info(inText[1]);
You can see my response to your previous post. It strongly believe it works for your use case.