Notepad++ find string in parantheses - regex

I try to fetch the string modlgn_username inside the paranthesis from the following string:
$this->webDriver->findElement(WebDriverBy::id("modlgn_username"))->click();
This is my regular expression:
\$this->webDriver->findElement\(WebDriverBy::id\("([A-z0-9]+)"\)\)->click\(\);
However I get Find: Can't find text \$this->webDriver->findElement(WebDriverBy::id("([A-Za-z0-9])"))->click();
It works in online regex tester though:
https://regex101.com/r/6oDry3/1

Your regex will be like this
(?<=\(\")\w+_\w+(?=\"\))
it checks that from left side of the string will be (" and from right side will be ").
It mach the string you need. Have a look

You have to add underscore in the character class or your regex and A-z is not a correct range, it inludes [\]^_ and backquote:
\$this->webDriver->findElement\(WebDriverBy::id\("([A-Za-z0-9_]+)"\)\)->click\(\);
and this character class can be now reduce to \w:
\$this->webDriver->findElement\(WebDriverBy::id\("(\w+)"\)\)->click\(\);

Related

Regex: How to find a string, then get charactes on either side up to a dilimeter?

I have a string like so:
foobar_something_alt=\"Brownfields1.png#asset:919\" /><p>MSG participat
And wish to find all oocurrences via the substring #asset: then select the characters around the match up to the quote marks.
Trying to extract specific ALT tags from a SQL dump. Is this possible with a regular expression?
Put [^"]* before and after the string you want to match. This will match any sequence of characters that aren't ".
[^"]*#asset:[^"]*

How to match begin of expression and exclude it?

I've a string SRE("Documents", with: "For boss") and I want to match string inside "" but without "".
I've come to regex \"([a-zA-Z0-9\s?]+) but in result I can see ["Documents, "For boss].
How the regex should looks like to exclude also first "?
Try "([^"]*)", where [^"] means matching a single character which is not ".

RegEx to find and replace a pattern in SublimeText

I need to use a Regular Expression in SublimeText for a Find and Replace search.
My search string pattern looks like this:
chrome.i18n.getMessage("nimbusBtnLogin")
in which case I need to replace with:
"nimbusBtnLogin"
I have a GIST here https://gist.github.com/jasondavis/fda85e808a6c8184adad where I have listed the RegEx for a find and replace of HTML form selection option fields, links, and images however I was unable to modify and get working for this pattern above.
Can someone please share the correct RegEx?
You can use
\bchrome\.i18n\.getMessage\("([^"]*)"\)
Replace with $1. See the regex demo
Note:
\bchrome\.i18n\.getMessage\(" - matches literal string (matched as a whole word) chrome.i18n.getMessage(" (special characters are escaped since they must be treated as literals)
([^"]*) - matches and captures into Group 1 any characters other than "
"\) - matches literal ")

Groovy : RegEx for matching Alphanumeric and underscore and dashes

I am working on Grails 1.3.6 application. I need to use Regular Expressions to find matching strings.
It needs to find whether a string has anything other than Alphanumeric characters or "-" or "_" or "*"
An example string looks like:
SDD884MMKG_JJGH1222
What i came up with so far is,
String regEx = "^[a-zA-Z0-9*-_]+\$"
The problem with above is it doesn't search for special characters at the end or beginning of the string.
I had to add a "\" before the "$", or else it will give an compilation error.
- Groovy:illegal string body character after dollar sign;
Can anyone suggest a better RegEx to use in Groovy/Grails?
Problem is unescaped hyphen in the middle of the character class. Fix it by using:
String regEx = "^[a-zA-Z0-9*_-]+\$";
Or even shorter:
String regEx = "^[\\w*-]+\$";
By placing an unescaped - in the middle of character class your regex is making it behave like a range between * (ASCII 42) and _ (ASCII 95), matching everything in this range.
In Groovy the $ char in a string is used to handle replacements (e.g. Hello ${name}). As these so called GStrings are only handled, if the string is written surrounding it with "-chars you have to do extra escaping.
Groovy also allows to write your strings without that feature by surrounding them with ' (single quote). Yet the easiest way to get a regexp is the syntax with /.
assert "SDD884MMKG_JJGH1222" ==~ /^[a-zA-Z0-9*-_]+$/
See Regular Expressions for further "shortcuts".
The other points from #anubhava remain valid!
It's easier to reverse it:
String regEx = "^[^a-zA-Z0-9\\*\\-\\_]+\$" /* This matches everything _but_ alnum and *-_ */

Boost:regex_search - match string between brackets

given this part of a LogString:
... HTTP/1.1" 206 391402 "-" "AppleCoreMedia/1.0.0.8B117 (iPod; U; CPU
OS 4_1 like Mac OS X; de_de)"
I need to match the last substring that is inside the brackets to get the Remote System Info from the string.
I'm not really into regular expressions but from my understanding I have to escape the brackets like "\(" and "\)".
Now to match the whole string I thought I'd have to do it like:
\(\.*\) with the dot matching anything and "*" for specifying a random amount of dots.
But it does't work. Neither does (\.*) or (.*) as you would expect.
There must be a problem with opening and closing the brackets because I get a Seg Fault when trying to match the string this way.
Can anyone help me please?
Ok sorry for bothering you. This did the trick:
boost::regex f_sourceDir(conf.pString("filter_SourceFiles")),
f_string(conf.pString("filter_String")),
m_first(conf.pString("field0")),
m_second("\\(([^)]+)\\)\"$",boost::regex::perl),
m_third(conf.pString("field2")),
m_fourth(conf.pString("field3"));
Seems to be a problem with parsing the configuration, it can't handle such strings. I think I have to change the Config class...
Thx anyway!
Try this
\(.*\)
See it here on Regexr
You need only to escape the brackets, but not the .. If you escape the . then you will match a literal dot and this will not be found.
If you not want to have the brackets then here is a solution using a capturing group
\((.*)\)
See it here on Regexr
You find the part inside the brackets stored in the capturing group 1.
Just in case, there are two ways of handling a string with a regex: matching and searching. Matching either matches the whole string or fails, searching does not have to match the whole string. What you are doing is searching.
The expression you need using Perl syntax (boost::regex let's you choose the syntax) is something like:
\(([^)]+)\)"$
That is: find a substring of one or more non-right-bracket symbols within brackets followed by the trailing " and the end of the string.
In C++ you need to quote this expression:
boost::regex re("\\(([^)]+)\\)\"$", boost::regex::perl);