Extracting String using regex - regex

I am using a HTA Application I wrote for our help desk to take notes.
I've been using regex (Best I can) to CTRL+A our ticket pop up and click parse on my app to fill out information
I need to find "TICKET - T00000000.0000 - Account Security (Company Name...)" and only grab the "Account Security" section. or for future grab whatever is between the 2nd - and the (
Any suggestions would be grand
here is an example what I've tried and what I am using
try {
$(".problem_description", context).val(clipdata.match(/TICKET -.+[)]/)[0]);
}
catch (e) {
}
Update
I have tried a few of the suggestions here but the results still seem to give me the entire string or error out in my script.

Here's the regex using positive lookbehind:
(?<=TICKET\ -\ T\d{8}\.\d{4}\ -\ ).*\)
Here's regex101 explanation: https://regex101.com/r/6BN16e/1
The query effectively says matching anything after "TICKET - T(8 digits).(4 digits) - ". You can of course tweak it to your specification.
Here's a tutorial on lookahead and lookbehind that may be helpful: https://www.regular-expressions.info/lookaround.html

Use a capture group. In a regex you can use parentheses to mark a capture group. So if you define a pattern where a portion of it marks the text you want to extract, you can wrap that portion in parentheses. The object returned by the match function in most languages is an object that lets you access the values of individual capture groups.
Try this regex I quickly made up: /[^-]*-[^-]*- ([^(]*)/
Full example: var matches = "TICKET - T00000000.0000 - Account Security (Company Name...)".match(/[^-]*-[^-]*- ([^(]*)/)
Your value will be in matches[1].
It says: start from the beginning, look for anything not a dash, then a dash, then anything not a dash, then another dash, then a space, then capture anything not a left-parenthesis into a capture group.
This one will leave an extra space at the end of the captured group value. Also, it will truncate your value if your value contains a left parenthesis.

Related

RegEx Replace - Remove Non-Matched Values

Firstly, apologies; I'm fairly new to the world of RegEx.
Secondly (more of an FYI), I'm using an application that only has RegEx Replace functionality, therefore I'm potentially going to be limited on what can/can't be achieved.
The Challange
I have a free text field (labelled Description) that primarily contains "useless" text. However, some records will contain either one or multiple IDs that are useful and I would like to extract said IDs.
Every ID will have the same three-letter prefix (APP) followed by a five digit numeric value (e.g. 12911).
For example, I have the following string in my Description Field;
APP00001Was APP00002TEST APP00003Blah blah APP00004 Apple APP11112OrANGE APP
THE JOURNEY
I've managed to very crudely put together an expression that is close to what I need (although, I actually need the reverse);
/!?APP\d{1,5}/g
Result;
THE STRUGGLE
However, on the Replace, I'm only able to retain the non-matched values;
Was TEST Blah blah Apple OrANGE APP
THE ENDGAME
I would like the output to be;
APP00001 APP00002 APP00003 APP00004 APP11112
Apologies once again if this is somewhat of a 'noddy' question; but any help would be much appreciated and all ideas welcome.
Many thanks in advance.
You could use an alternation | to capture either the pattern starting with a word boundary in group 1 or match 1+ word chars followed by optional whitespace chars.
What you capture in group 1 can be used as the replacement. The matches will not be in the replacement.
Using !? matches an optional exclamation mark. You could prepend that to the pattern, but it is not part of the example data.
\b(APP\d{1,5})\w*|\w+\s*
See a regex demo
In the replacement use capture group 1, mostly using $1 or \1

Regex formula to scope a group and pull a value from it

I have a quick inquiry about a regex formula, I am trying to pull group membership via a regex function, in my task I have for example a group named SSO-TEST-Admin,
I would like to know if I can filter the group via something like:
.*SSO-TEST-.* basically scoping all groups matching the SSO-TEST-, after I get the group, transform the result to give only the Admin portion from it or whatever value is after the SSO-TEST-.
I have tested: (?(?=(SSO-TEST*))SSO-TEST-ADMIN|SSO-TEST-ReadOnly-admin), but was unsuccessful. Appreciate any hint.
Use a positive lookbehind to match the SSO-TEST- prefix and return the words after it.
(?<=SSO-TEST-)[\w\-]+
[\w\-]+ will match word characters and hyphens after the prefix.

Using regex to make edits on multiple lines

I have downloaded an sql file and would like to mass replace some names of pages I have created.
This is an example of one page:
{\"ref\":\"Session_1___Pg1___V2\",\"pageTitle\":null,\"description\":null,\"revealDate\":0,\"gQRef\":null,\"lQRef\":null,\"gQScore\":null,\"lQScore\":null,\"newsfeedDates\":null,\"subtitle\":null,\"pageLinkTitle\":null,\"linkTitle\":null,\"pageBack\":null,\"pagePrint\":false,\"visitedFlag\":null,\"widthPercentage\":0,\"maxWidth\":0,\"thumbnail\":null,\"edit\":null,\"copy\":null,\"delete\":null,\"preview\":null}]}
How do I search and highlight all the references of each page, just like Session_1___Pg1___V2 from above, without selecting anything else. I have hundreds of pages that I need to change the references of and I think regex would be the best way to do it with.
I used (\"((.*?))\") but it would select everything that is inbetween quotes. How do I just select the ref of the pages?
Use a lookbehind:
(?<=\\"ref\\":\\)"([^"]+)"
(?<=\\"ref\\":\\) Lookbehind for \"ref\":\ substring.
"([^"]+)" Matches ", opens capture group, capturing anything other than a ". Then closes capturing group and matches ".
The result is group:
Group 1. 11-32 `Session_1___Pg1___V2\`
Regex demo here.

REGEX help to capture certain values from string

I am hoping someone can assist with the REGEX I am trying to do. I just want to be able to capture the first group of characters immediately after either "Job" or "Job -".
EXAMPLE:
Job PXDFUH34 RE443 JRA99
Job - W0WEIN12SD UIS90 TYPSOS48
I want to only capture PXDFUH34 and W0WEIN12SD in this example.
UPDATE
I was able to use this to capture what I needed.
\s(\w+)\s
However, I ran into a special character (#) that this regex doesn't like. How do I account for # now?
EXAMPLE:
Job R#DFUH34 RE143 JRU89
Job - W0WEIN12SD# UIS10 TTPSOS45
Try this regex:
Job\b[\s-]*(\S+)
It means:
Look for Job and a limit \b - to avoid text like Jobless
and [\s-] spaces and hyphens * as many as possible you can find,
and then group ()
the first word \S+.
Regex live here.
Hope it helps.
Try this regex
^Job\s\-?\s?\K[^\s]*\b
On the basis of #alanmoore comments this is the alternative
^Job\s\-?\s?([^\s]*)\b
Working Regex

regex to find value at a particular location

Presently the regex is:
[A-Z]+(?=-\d+$)
This pulls out the correct value for most of the strings which follow the below format:
ANG-RGN-SOR-BCP-0004 i.e. BCP
However it pulls out SS for the following document instead of PMR:
ANG-B31-OPS-PMR-MACE-SS-0229
So basically I want to pull out the fourth term (between the hyphens), so it should pick BCP and PMR.
The following regex will get the 4th item in group 1:
(?:[A-Z0-9]+-){3}([A-Z0-9]+)
The first bit in (?:...) is a "non-capturing group" which acts like a group but won't appear in the backreference list.
The next bit means "3 of these non-capturing groups".
And finally, a capturing group to collect what you want.
I have assumed here that all the groups contain only uppercase letters and digits, you should modify the parts in [square brackets] to represent what these groups could be.
A more easily understandable method in Python:
a = "ANG-B31-OPS-PMR-MACE-SS-0229"
part = a.split('-')[3]
print part
This gives "PMR".
This should suit your needs (demo):
(?:.+?-){3}([^-]+)
You'll be able to access the fourth term in the first capturing group.