Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
RegEx is a bit confusing to me and I have been at it for 3.5 hours on one line.
I have a string:
" Layer 1 Layer 2 Layer 3 Layer 4 "
I would like to split it up into a List and cannot get it to work.
I tried this and it was close but still not what I am looking to do:
List<string> lineWords = Regex
.Matches(line, #"[Layier_]*\s*\s*[1-9]")
.OfType<Match>().ToArray()
.Select(match => match.Value)
.List();
Where am I in error?
Thank you.
You didn't write it explicitly, but I assume, you are using C#.
Your code requires only minor corrections:
I changed the regex a little (compare).
ToArray() is not needed.
Instead of List() use ToList().
So the code given below works.
List<string> lineWords = Regex.Matches(line, #"[Layier_]+\s*[1-9]")
.Cast<Match>()
.Select(match => match.Value)
.ToList();
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to come up with a regex to extract certain pieces of information from the below log. I've been messing with regex groups for awhile and am not getting anywhere. I would like to get the date/timestamp, the usr field, dstname field, and arg field. How can I accomplish this?
May 1 08:21:02 192.168.1.1 id=firewall sn=fakeserial time="2020-05-01 12:21:02 UTC" fw=1.2.3.4 pri=3 c=4 m=14 msg="Web site access denied" app=2515 sess="Auto" n=398533 usr="sampledomain\username" src=192.168.1.150:50334:X0 dst=72.21.81.240:80:X1 srcMac=b0:00:b4:18:4a:b5 dstMac=c0:ea:e4:9d:a0:8c proto=tcp/http dstname=ctldl.windowsupdate.com arg=/msdownload/update/v3/static/trustedr/en/disallowedcertstl.cab code=99 Category="Administrative Custom List settings" fw_action="drop"
You could base it on something like this
(\w{3} \d* [\d:]*).*usr="([a-z\\]*)".*dstname=([\w.]*).*arg=([\/][^ ]*)
https://regex101.com/r/VUIBAm/1
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Ive been trying for ages to get the id 30393 from the below string.
https://example.com/service_requests/30393/journey
Any ideas how? Its the / causing me issues. Been trying (?<=/).*?(?=/) but obviously it doesn't work.
You could try this:
\d+
DEMO: https://regex101.com/r/9JyTdx/1
OR
(?<=service_requests\/)\d+(?=\/journey)
DEMO: https://regex101.com/r/8SXJiJ/1
/^https:\/\/example.com\/service_requests\/(\d*)\/journey$/
The answer is this (?<=service_requests\/).*?(?=\/)
Some of the comments inspired me. Just made an alteration to #bhusak comment.
This might not be a job for regexes, but for existing tools in your language of choice. Regexes are not a magic wand you wave at every problem that happens to involve strings. You probably want to use existing code that has already been written, tested, and debugged.
In PHP, use the parse_url function.
Perl: URI module.
Ruby: URI module.
.NET: 'Uri' class
Split the string on / characters and return the 2nd-to-last element. Here's the JavaScript solution, but it should be similar in other languages.
let url = 'https://example.com/service_requests/30393/journey';
let arr = url.split('/');
let id = arr[arr.length-2];
console.log(id);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a string
abc = 'tea,coffee'
Want to split into ('tea','coffee')
I tried to split but the result is showing as double quotes in front and last
i am getting result as "tea','coffee"
#given string
abc = 'tea,coffee'
#splitting into array
result = abc.split(',')
#making tuple
tuple(result)
#printing result
print(result)
done!!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
So I have one modified html file that has some links in it, and I want to extract them (grep or similar) so I only have links that start with http://* and end with .epub, the extension).
I tried some solutions here on stackoverflow, but none seems to work as I can't seem to extract anything.
How would I go in doing this?
EDIT: The links are laid out on the file like this as well: > http://........epub" class="..."><i but I just want to extract everything between http and .epub, including those 2.
grep -o 'http://[^ "<]*.epub' file.html should do the trick
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
I am trying to figure out a regex to strip extra single quotes so that I would end up with only one single quote. To explain my question better, here is an example.
Let's say I have 3 different strings such as this ones.
(two single quotes)
Name<span fontSize=''16'' baselineShift=''superscript''>ABC</span>
(three single quotes)
Name<span fontSize='''16''' baselineShift='''superscript'''>ABC</span>
(four single quotes)
Name<span fontSize=''''16'''' baselineShift=''''superscript''''>ABC</span>
I am trying to sanitize the string to end up with this:
Name<span fontSize='16' baselineShift='superscript'>ABC</span>
I tried several online tools. This one is my favourite one: http://ryanswanson.com/regexp/#start. But I just can't get it right.
Could someone please help me out? Any tips and suggestions would be greatly appreciated.
Thank you in advance!
Did you try '+?
var str:String = "Name<span fontSize=''''16'''' baselineShift=''''superscript''''>ABC</span>";
trace( str.replace(/'+/g, "'") );
Have you looked at the docs for AS3's RegEx code? AS3 Replace
You could try something like this
var myPattern:RegExp = /'{2,100}/g;
var str:String = "fontSize=''''16''''";
trace(str.replace(myPattern, "'"));
The '{2,100} essentially looks for a match of ' that occurs between 2 - 100 times and replaces it with a single '.