My regular expression is like this:
.*(kgrj4e|\*)[^:]*:([^;]*);?
The 'kgrj4e' part is a userid and is dynamic. The PR.... parts are printers. If the userid is not found I want the default printer (PR12346).
For first test string below I want result to be PR12345, but I get PR12346
snljoe,snlaks,kgrj4e,snlbla:PR12345;*:PR12346
Note: the users snljoe, snlaks and snlbla are just examples and can be totally different. In fact the list of users can be longer or smaller.
For second test string below I want result to be PR12346
snljoe,snlaks,snlbla:PR12345;*:PR12346
How to fix the regular expression so both test strings give the expected result?
You can get the number with a search and replace:
Search for: ^(?:(?!.*,kgrj4e(?:[;,])).*\*:(\w+)|.*?(PR\d+).*)
Replace with: $1$2
See this demo
I assume that the kgrj4e is a user-defined value that should be missing in the string to match the last printer value. If it is present, the first printer value is returned.
Related
I am capturing the session id from the string. I want to add word(prefix) before the extracted session id.
Sample input: key=this is sample input; MySessionId=hhjsfd436763jhjhfdjs87787.hghht77f54; key7=jhu8787; type=raw; oldkey=jkjf8787;
I have formed the below regex to capture the MySessionId.
MySessionId=([^.]*)
I want to add a word before the extracted string like below.
Expected output:
ABCD-1234-hhjsfd436763jhjhfdjs87787
Any way to achieve this through Regular expression?
It really depends what language you're using, you'll need to find a function that replaces text in a string (usually it's called replace). It looks like you're dealing with cookies so I'll show you an example in javascript:
//$1 refers to the first group captured by the regex
//i think other languages use $1 too but you should probably check
string = string.replace(/MySessionId([^.]*)/, "ABCD-1234-$1")
I'm using an application called Firemon which uses regex to pull text out of various fields. I'm unsure what specific version of regex it uses, I can't find a reference to this in the documentation.
My raw text will always be in the following format:
CM: 12345
APP: App Name
BZU: Dept Name
REQ: First Last
JST: Text text text text.
CM will always be an integer, JST will be sentence that may span multiple lines, and the other fields will be strings that consist of 1-2 words - and there's always a return after each section.
The application, Firemon, has me create a regex entry for each field. Something simple that looks for each prefix and then a return should work, because I return after each value. I've tried several variations, such as "BZU:\s*(.*)", but can't seem to find something that works.
EDIT: To be clear I'm trying to get the value after each prefix. Firemon has a section for each field. "APP" for example is a field. I need a regex example to find "APP:" and return the text after it. So something as simple as regex that identifies "APP:", and grabs everything after the : and before the return would probably work.
You can use (?=\w+ )(.*)
Positive lookahead will remove prefix and space character from match groups and you will in each match get text after space.
I am a little late to the game, but maybe this is still an issue.
In the more recent versions of FireMon, sample regexes are provided. For instance:
jst:\s*([^;]?)\s;
will match on:
jst:anything in here;
and result in
anything in here
I'd like to match an input given by user (String) with a value (String) of a specific node existing in rdf file.
I applied the following exact mode for matching (input=NodeValue):
...
FILTER regex (?NodeValue,"userinput$","i").
for this type of matching (input < NodeValue ) I used the following:
...
FILTER regex (?NodeValue,".*userinput.*","i").
So, my question is how to set my regex in order to get the type of matching when (input > NodeValue) I mean a query that's returns a list of ?nodeValue subsumed by a given user input.
Eg. if the user enters patagoniaisbeautiful it returns patagonia.
Thank you in advance.
To achieve a match where the database value is a substring of your user input, you need to flip your arguments for the regex function around. That way, the actual value in the database is used as the regular expression, and the user input as the string to match it:
FILTER(REGEX("patagoniaisbeautiful", STR(?NodeValue), "i"))
This will succeed if ?NodeValue is "patagonia". Of course it will also match if ?NodeValue is "p", "a", "t", etc.
In fact, given that you are only interested in simple substring matching here, you can simplify this by using the CONTAINS function, instead of the (computationally expensive) REGEX operation. Like so:
FILTER(CONTAINS("patagoniaisbeautiful", LCASE(STR(?NodeValue))))
As an aside: you give an example of doing a regex where the user input is a substring of the database value: ".*userinput.*". The leading and closing .* here are unnecessary. A SPARQL regex match is by definition a substring match.
Inside openRefine I want to run the below regex on a website's source that finds email addresses with a mailto link. My trouble is when running value.match, I get this error:
Parsing error at offset 12: Bad regular expression (Unclosed character class near index 10 .*mailto:[^ ^)
I have tested the expression in another environment without value.match and it works.
value.match(/.*mailto:[^/"/']*.com.*/)
So if you have text like:
Blah blah mail me
To extract the email address using the match function in OpenRefine you need to use:
value.match(/.*mailto:([^\"\']*.com).*/)
This will give an array containing the email address, which is captured using a capture group. To extract the email address from the array (which is necessary if you want to store the mail address in an OpenRefine cell) you need to get the string value from the array. e.g.:
value.match(/.*mailto:([^\"\']*.com).*/)[0]
The difference between your original expression and this one is that the characters are escaped correctly and there is a capture group - basically implementing the advice from #LukStorms in the comments above.
isNotNull(value.match(/.*mailto:[^\"\']*.com.*/))
as described on our Reference page for the match() function, it return an array of capture groups in your RegEx pattern and then isNotNull() outputs True or False if your value is like that pattern:
https://github.com/OpenRefine/OpenRefine/wiki/GREL-String-Functions#matchstring-s-regexp-p
also described here: https://github.com/OpenRefine/OpenRefine/wiki/Understanding-Regular-Expressions#basic-examples
You can also use get() as described here in Recipes on our wiki, BUT will only work well if you have only 1 email address per cell (its because the get() function without number from or to, makes assumptions and uses the length of the array to determine the last element and pushes out only the last element, not the first, or third, etc.):
https://github.com/OpenRefine/OpenRefine/wiki/Recipes#find-a-sub-pattern-that-exists-at-the-end-of-a-string
For example:
get(value.match(/.*(mailto:[^\"\']*.com).*/),0)
I want to find a specific number from a HTML response.
For example, I want to extract 3 from publicationID3publicationID.
Does someone know a solution with regexp?
Add Regular Expression Extractor Post Processor as a child of the request, which returns to you this string.
Configure it as follows:
Reference Name: publicationID (you can use any variable name here)
Regular Expression: publicationID(\d+)publicationID
Template: $1$
other fields can be left blank.
You can later refer publication ID as ${publicationID} or ${__V(publicationID)}
You can see what matches does your Regular Expression return using View Results Tree Listener (select RegExp Tester from dropdown). Another option is Debug Sampler again with combination with View Results Tree.
you can use \d to match a number using regex.