Regex to match ids in a css file? [closed] - regex

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
With the following input:
#element{ color: #333; }
#element2,
.element3,
.whatever{ background: red; }
.test,
.test2, .test3,
#test111,
.element5555, #fooooooter{ display: none; }
#element8 tr .bacon{ font-size: 10000em; }
How do I use regex to match these:
#element
#element2
#element8
#test111
And not match these:
#333
I tried more variations so far and I can't come up with anything useful.
This is what I tried the latest:
(#).*[{]
But it seems to skip some selectors.

#-?[_a-zA-Z]+[_a-zA-Z0-9-]*(?=[^}]*\{)
It explicitly matches valid characters in CSS selector. Which characters are valid in CSS class names/selectors?
http://regexr.com?36e6v
Don't forget to remove comments and quotation like content: '{}' before applying this regex.
Matching comments and quotations is possible with regex. Remove it using unrolling skill, separately.
(I think unrolling is complicated and inefficient enough to rule the integration out.)
It does match color if {} blocks are nested as are in Sass, less

OK,Check out this one,it exactly matches what you need;
^\#[a-z]+[0-9]* //(Multiline = true)
If you need to capture ID Name,use this;
^\#(?<IDName>[a-z]+[0-9]*) //(Multiline = true)
This regex has a capture group named IDName which gives you name of the ID.
Demo here.

Okay, I think I got it (http://regexr.com?36e5i):
#[^{,;]*(?:[{,\n]) will also match ids followed by other selectors (eg., #element8 tr .bacon)
#[^ {,;]*(?:[ {,\n]) will only match ids only (so you'd get the #element8 part from the example above, but not the rest)

try this /#[^1-9]+/g
var t=" #element ";
var p=t.search(/#[^1-9]+/g);
alert(p);
or /#[^1-9.##$%^&*()!]+/g if want to remove more symbols other then numbers

Upadated based on a comment
This will work for you:
/(?![^{]+})(#\S+)\b/g
Check it here: http://regex101.com/r/rF9iR9

Related

How to validate substring using regex [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I am trying to validate whether my API header is in PascalCase or not, for this, I am using the following regex:
^([A-Z][a-z]*)(-[A-Z0-9][a-z0-9]*)*$
I have a use case where the API contains some keywords that are not in PascalCase and I have to ignore this while validating the header, for example:
X-CUSAPI-Trace-Id While using the above regex I want to ignore the X-CUSAPI- part and then apply the validation on Trace-Id.
I am trying with the following regex but it is not working.
^(?!.*X-CUSAPI-).([A-Z][a-z]*)(-[A-Z0-9][a-z0-9]*)*$
Adding example of API specification:
{
"in": "header",
"name": "X-CUSAPI-Trace-Id",
"description": "TraceID",
"required": false,
"schema": {
"type": "string"
}
}
Spotlight spectral tool which help to parse the JSON and find the name for me to apply the regex to validate either it is PascalCase or not:
header-should-pascal-case:
message: "Request and Response HTTP Header should be pascal-case, separated by hyphens Example: PascalCase-Header"
description: SHOULD prefer hyphenated-pascal-case for HTTP header fields
severity: error
given: $.paths.*.*.parameters[?(#.in=='header')].name
then:
function: pattern
functionOptions:
match: ^.*?(?:X-CUSAPI-)?((?:[A-Z][a-z]+)+(?:-(?:[A-Z0-9][a-z0-9]+)+))\b
The issue with the regex you are using is that the negative lookahead assertion is not correctly placed. You can modify your regex to achieve the desired result by placing the negative lookahead assertion before the beginning of the string anchor (^), like this:
^(?!.X-CUSAPI-)([A-Z][a-z])(-[A-Z0-9][a-z0-9])$
This will ignore any string that contains "X-CUSAPI-" anywhere in it, and then match the remaining string using the PascalCase validation pattern.

How to find an element that doesn't match a specific text with Capybara? [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 3 years ago.
With Capybara I am used to searching first('div', text: 'asdf'), but is there a way to search for elements that do not have the text?
I tried first('div', text: /^(?!asdf)/) and it still matches on div with 'asdf' text.
I checked to make sure my regex is right though: https://rubular.com/r/CN6MYKJNahiohD
Any ideas?
Without knowing what the text content is of the actual element being returned it's tough to say what's wrong, but negative regexes get complicated (especially if the element has multiple lines of content) so it's pretty safe to assume the issue is your regex. In cases like this sometimes it's easier to just use a filter block
first('div') do |element|
!element.text.include?('asdf')
end
Note: Capybara can't optimize this so it may not be as efficient, but for occasional use that's not always important.
Not sure about Capybara, just guessing that you might be trying to design an expression that would exclude asdf with word boundaries in a string, maybe close to:
^(?!.*\basdf\b).*$
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.
Test
re = /^(?!.*\basdf\b).*$/s
str = 'some content before, then asdf as a word, some after
some content before, then noasdf as a word, some after'
str.scan(re) do |match|
puts match.to_s
end
My current workaround for this:
selected_element =
(all('div').select do |d|
d.has_no_text?('asdf', wait: false)
end).first
It will return nil if none are found.

Converting a Regex Expression that works in Chrome to work in Firefox [duplicate]

This question already has answers here:
Negative lookbehind equivalent in JavaScript
(11 answers)
Closed 3 years ago.
I have this Regex Expression that works in chrome but doesn't not work in Firefox. SyntaxError: invalid regexp group
It has something to do with lookbehinds and Firefox does not support these. I need this to work in Firefox can some one help me convert this
so it works in Firefox and filters out the tags as well?
return new RegExp(`(?!<|>|/|&amp|_)(?<!</?[^>]*|&[^;]*)(${term})`, 'gi');
};
searchTermsInArray.forEach(term => {
if (term.length) {
const regexp = this.regexpFormula(term);
newQuestion.qtiData.prompt = newQuestion.qtiData.prompt.replace(regexp, match => {
return `<span class="highlight">${match}</span>`;
});```
In chrome it filters out the html tags and returns the search term with a <span class="highlight">.
You could try to solve things without using a negative lookbehind: do the opposite, match what you do not want as well. If it is there in your callback, then make sure to not highlight.
Note: I am not sure what the negative lookahead is accomplishing at the beginning, as you could easily make sure that the search term doesn't start with the listed values and that would yield the same result, so I am letting this part aside.
let regexp = new RegExp(`(&[^;]*|</?[^>]*)?(${term})`, 'gi');
haystack.replace(regexp, (match, ignore, term) => ignore ? match : `<span class="highlight">${term}</span>`);

Selecting URLs using RegExp but ignoring them when surrounded by double quotes

I've searched around quite a bit now, but I can't get any suggestions to work in my situation. I've seen success with negative lookahead or lookaround, but I really don't understand it.
I wish to use RegExp to find URLs in blocks of text but ignore them when quoted. While not perfect yet I have the following to find URLs:
(https?\://)?(\w+\.)+\w{2,}(:[0-9])?\/?((/?\w+)+)?(\.\w+)?
I want it to match the following:
www.test.com:50/stuff
http://player.vimeo.com/video/63317960
odd.name.amazone.com/pizza
But not match:
"www.test.com:50/stuff
http://plAyerz.vimeo.com/video/63317960"
"odd.name.amazone.com/pizza"
Edit:
To clarify, I could be passing a full paragraph of text through the expression. Sample paragraph of what I'd like below:
I would like the following link to be found www.example.com. However this link should be ignored "www.example.com". It would be nice, but not required, to have "www.example.com and www.example.com" ignored as well.
A sample of a different one I have working below. language is php:
$articleEntry = "Hey guys! Check out this cool video on Vimeo: player.vimeo.com/video/63317960";
$pattern = array('/\n+/', '/(https?\:\/\/)?(player\.vimeo\.com\/video\/[0-9]+)/');
$replace = array('<br/><br/>',
'<iframe src="http://$2?color=40cc20" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');
$articleEntry = preg_replace($pattern,$replace,$articleEntry);
The result of the above will replace any new lines "\n" with a double break "" and will embed the Vimeo video by replacing the Vimeo address with an iframe and link.
I've found a solution!
(?=(([^"]+"){2})*[^"]*$)((https?:\/\/)?(\w+\.)+\w{2,}(:[0-9]+)?((\/\w+)+(\.\w+)?)?\/?)
The first part from (? to *$) what makes it work for me. I found this as an answer in java Regex - split but ignore text inside quotes? by https://stackoverflow.com/users/548225/anubhava
While I had read that question before, I had overlooked his answer because it wasn't the one that "solved" the question. I just changed the single quote to double quote and it works out for me.
add ^ and $ to your regex
^(https?\://)?(\w+\.)+\w{2,}(:[0-9])?\/?((/?\w+)+)?(\.\w+)?$
please notice you might need to escape the slashes after http (meaning https?\:\/\/)
update
if you want it to be case sensitive, you shouldn't use \w but [a-z]. the \w contains all letters and numbers, so you should be careful while using it.

Extract string using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a string like
1372110902.747405 29245 verbose [paymentserv]: === AUTH: ExternalFundingAnalysisStage was successful (rc:0) ===
i want only
AUTH: ExternalFundingAnalysisStage was successful (rc:0)
using regular expression. please help me to sort out the issue.
Thanks in advance
Using PHP:
$var = "1372110902.747405 29245 verbose [paymentserv]: === AUTH: ExternalFundingAnalysisStage was successful (rc:0) ===";
$var = preg_replace("#.*===([^=]+)===.*#i",$1,$var);
Since you didn't specify which language you are using your regex in, any language is presumably a valid response. So, in Perl, you could use the substitution:
s/.*=== (.*) ===.*/$1/;
or the match:
m/=== (.*) ===/;
After the match, $1 will contain the string you wanted to find.
The .* after the second === in the substitute is unnecessary for the sample line of input, but is symmetric with the .* at the beginning of the regex, and symmetry is pleasing. It protects the substitute command from trailing debris; the match doesn't need the protection.
If it's always going to be along the same lines (beginning with AUTH, ending with (rc:#)):
//JavaScript
var str = "1372110902.747405 29245 verbose [paymentserv]: === AUTH: ExternalFundingAnalysisStage was successful (rc:0) ===";
str = str.slice(str.indexOf("AUTH"), str.lastIndexOf(")")+1);
Or with a regex:
str = str.replace(/^.+={3}\s(.+)\s={3}$/,"$1");
for that you also use java regex for detail follow this Tutorial and in your case you can also use sub-string .
Like:
String str ="1372110902.747405 29245 verbose [paymentserv]: === AUTH: ExternalFundingAnalysisStage was successful (rc:0) ===";
String str1=str.subString(58,106);
Stirng str2=str.replace("(","");
Then you get your required answer