regular expression ~ extract string - regex

I need to extract using Regular Expression from following string
console.log("This can be anything except double quote"),
followed by comma and any other string
and the extraction output is
console.log("This can be anything except double quote"),
Note that the sample string shall not be read literally (e.g. can be anything means a random string or symbol
~!##$%^&*)
Any idea, what is the right regular expression for above case?

Using Regex for quoted string with escaping quotes:
(console\.log\("(?:[^"\\]|\\.)*"\),)

There are many solutions to this.
The simplest I could think of: (console.log[^,]+)
PS: This removes the comma at the end of the console statement. You can manually add that.

Related

Regex Expression - text between quotes and brackets

i have the following JSON string that i need to parse:
{'ConnectionDetails':'{\'server\':\'johnssasd02\',\'database\':\'enterprise analytics\'}'}]}
i am already using the expression '([^']*)' to get everything in quotes, which correctly gets me the ConnectionDetails title. However i now need an expression to get me everything between '{ and '} in order to get the full path value. so i need to capture the following from above string:
{\'server\':\'johnssasd02\',\'database\':\'enterprise analytics\'}
but having trouble coming up the regex expression
thanks
In order to extract the data between the curly braces {} you can use the regex: \{(.*?)\}
i accomplished it within an SSIS derived column task where i removed unwanted characters from the input string. that way i don't have to worry about dealing with them using regex.

Regular expression and extracting a value

I need to get hold oif the |value| below:
{"token":"<input name=\"__RequestVerificationToken\" type=\"hidden\" value=\"KhWUxVIL697p18Gm3T1b4pCmXjK7iQujsJieYiLOKcKmKbdvC55kgaqg4G-uGqeUzmV3x6EMAV_ejPHe-Ok2kFqnjzVmvZmHySMpwKzGvq01\" />"}
What kind of regular expression would match that?
I have tried to us this:
.check(regex("input name='__RequestVerificationToken' type='hidden' value='([A-Za-z0-9+=/'-'_]+?)'").saveAs("token")))
But it does not match.
Also using a regex tester does not get me anywhere, please help me.
I would use something like this:
regex("<input.+__RequestVerificationToken.+value=\\?(\"|\')(.+)\\?(\"|\').+>")
It can be made shorter, but I was not sure how actual example string looks (does it have escape chars at once, does it use single or double qoutes).
assuming that the string in your question is exactly the way it appears, with escaped double quotes \" etc.
here is the code:
val regexGroupExtractor = """.*value=\\"(.*)\\".*""".r
val regexGroupExtractor(e) = s
// e == "KhWUxVIL697p18Gm3T1b4pCmXjK7iQujsJieYiLOKcKmKbdvC55kgaqg4G-uGqeUzmV3x6EMAV_ejPHe-Ok2kFqnjzVmvZmHySMpwKzGvq01"
In general with regex it is often helpful to think of the pattern in reverse: instead of specifying what is included, specify what is not. In your case there is no need to specify which characters are "in" inside the (), instead focus on where the part you want starts and ends. Specifically in your example - quotes are outside the string you want, in fact the quotes are exactly the edges, so in my regex I capture whatever it is between them.

how to define a regular expression in boost?

I have a section in file:
[Source]
[Source.Ia32]
[Source.Ia64]
I have created the expression as:
const boost::regex source_line_pattern ("(Sources)(.*?)");
Now, I am trying to match the string, but I am not able to match; it is always returning 0.
if (boost::regex_match ( sToken, source_line_pattern ) )
return TRUE;
Please note that sToken value is [Source]. [Source.Ia32]... and so on.
Thanks,
There are at least two problems with your code. First, the
regular expression you give contains the literal string
"Sources", and not "Source", which is what you seem to be
trying to match. The second is that boost::regex_match is
bound: it must match the entire string. What you seem to want
is boost::regex_search. Depending on what you are doing,
however, it might be better to try to match the entire string:
"\\[Source(?:\\.(\\w+))?\\]\\s*". Which provides for capture of
the trailing part, if present (but not the leading
"Source"---no point, in general, in capturing something that is
a constant).
Note too that the sequence ".*?" is very dubious. Normally,
I would expect the regular expression parser to fail if
a (non-escaped) '*' is followed by a '?'.
The issue is that boost::regex_match only returns true if the entire input string is matched by the regex. So the '[' and ']' are not matched by your current regex, and it will fail.
Your options are either to use boost::regex_search, which will search for a substring of the input that matches your regex, or modify your regex to accept the entire string being passed in.

Regular expression for format XX-XXXX

I want a regular expression for format xx-xxxx can somebody help? It will take numbers and
characters. I have no idea about regular expressions. also can some explain that how whole expression works?
Thanks
Assuming that the string will be only containing exactly that string with no leading or trailing whitespace:
/^[a-zA-Z0-9]{2}-[a-zA-Z0-9]{4}$/
If you need to find that pattern within another string (a paragraph?) you would use:
/\b[a-zA-Z0-9]{2}-[a-zA-Z0-9]{4}\b/
The expression:
\A[0-9a-zA-Z]{2}-[0-9a-zA-Z]{4}\Z
will also work.

How do I get the following regular expression to not allow blank e-mails?

I am using the following regular expression to validate e-mails, but it allows empty strings as well, how can I change it to prevent it:
^[\w\.\-]+#[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$
I am using an asp:RegularExpressionValidator. My other option is to add on a asp:RequiredFieldValidator, but I am curious if this is possible to check for blanks in my RegularExpressionValidator, so I don't have to have 2
see http://www.regular-expressions.info/email.html
That expression does not match empty strings. The expression starts with ^[\w\.\-]+ this translates to "The string must start with a word character, period or slash. There can be more than one of these." There must be something else wrong or you copied the expression incorrectly.
This RegEx validates if a given string is in a valid email-format or not:
/^[a-zA-Z0-9\_\-\.]+\#([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/