Quick Regex question - regex

Can anybody guide me in the right direction...
I have some folder strucktures that I want to check for a trailing / slash, some of the folders don't have a trailing / and if it does not have a trailing
/ I need to add one
If I use this regex it works but it replace the last character of the folder name
Folder/name/test
folder/name/test1/
folder/name/test2/
replace(/.$/ig,"/");
This regex replaces Folder/name/tes[t]/ but will take out the t and replace it with /
Hope this makes sense...

Try something like this:
replace(/[^/]$/ig, "$0/")

replace(/(.)$/ig,"\1/");
or better
replace(/([^\\])$/ig,"\1/");
if \1 isn't a backreference in your language, then you'll have to figure that out, or tell us teh language.

The regex you made basically means this: take any character that is the last one in the string and replace it with /. What you need to do is to group the last character and then insert it again in the replacement, like this:
replace(/([^\/])$/ig,"$1/");
For more information see
http://www.regular-expressions.info/brackets.html

Without knowing the language it's difficult to post a correct answer and you can't use the code provided in a cut-and-paste fashion. Anyway I might go for this regex:
replace(/(.)\/*$/,"\1/");
This will append the trailing / only if it's not there yet.

I'm not sure which language this is for but this is how you would do it in Perl:
#! /local/bin/perl
my #data = <data>;
while (<DATA>)
{
s#[^/]\n#/\n#m;
print;
}
__DATA__
/foo/bar/
/baz/jazz
/baz/jazz
This then prints out the following:
/foo/bar/
/baz/jaz/
/baz/jazz/
The key to the regex is the "[^/]\n" This basically matches anything at the end next to to the newline. With your nomenclature, I would assume the syntax would be the following:
replace(/[^\/]\n/ig,"/");
Or if there is no newline use this:
replace(/[^\/]$/ig,"/");
I hope that helps.

I would avoid a regular expressions in this case and do something easier like:
$path = rtrim($path, '/').'/';
EDIT:
Woops, assumed it was php...

Related

regix match words starting with dot but exclude some

I need to build regex to match all words starting with . but also white flag words like .well-known .. or some similar..
For now I have build the one that's complete opposite,, this one captures ONLY this one. I tried to find some regex symbol for invert but that doesn't exist I think..
location ~ /^(\.well-known) {
deny all;
}
thx
Here's an expression that works:
^(\.(?!well-known|other-forbidden-words|another-forbidden-word).+)$
Simply change the other words to white-listed words you want, and add more if need be.
hm,,
I guess that I found my answer thanks to:
Invert match with regexp
this is the test that works now:
https://regex101.com/r/mS2wC6/2
and solution is this:
^(\.(?!well-known))
so ok, I think I got this grouping thing now..
hth, k
well you don't have to add words in OR "|" condition
you can simply do
$pattern = "/\.+(?:[a-zA-Z]|-)/";
which accepts anything that starts with . and contains - or alphabets.

Regex to match part of file name

I need to use regex match expression to find part of a file name.
eg file name is ABC01-001-M-001_0.dwg
I need to match the first digit after the underscore (this is the revision number of the drawing file name)
In this case with the example it would match the 0
Can anyone please advise the regex for this?
Many thanks.
pushpraj answer is almost correct. I take it you dont want to see the underscore.
so (?<=_)(\d+) would be the correct choice. The (?<=_) says, that the underscore have to be in front of your desired pattern but not included.
Demo: https://regex101.com/r/kH1nE2/2
I think this should be pretty simple.
_(\d)
online demo https://regex101.com/r/kH1nE2/1
in case you expect more digits you can use the following
_(\d+)
note "+" which will match 1 or more digits after underscore
In this case regex is not even needed.
$n = "ABC01-001-M-001_0.dwg";
Echo intval(explode("_", $n)[1]); // 0
https://3v4l.org/fBXc5
It seems you are looking for a javascript solution?
var n = "ABC01-001-M-001_0.dwg";
var int = parseInt(n.split("_")[1]);
Not sure it actually works because I'm not doing writing in javascript but ^^ is what I could google about the functions.
Maybe works, maybe not.
It seems to work :-)
https://jsfiddle.net/w9x7d40f/

Regex until repeat character (uri path)

I've got such examples
/path/to/service/People("Peter")
/i/dont/care/about/how/much/pathes/we/have/here/Customer("John")
/itcouldbejustone/Client("Rick")
i need to regex and leave just People("Peter"), Customer("John"), Client("Rick") accordingly
i was trying to use:
\/.+?(?=\/)
but we have a lot of "/" slashes, how to avoid it? thanks
Make it greedy ....
\/.+(?=\/)
To match also the last /,
\/.+\/
DEMO
You can do this without regex if using PHP
$url = '/path/to/service/People("Peter")';
$name = end(explode("/", $url));
$name will have People("Peter");
That depends a bit on which regex tool you are using.
Anyway, there are many ways of getting that:
If you will never have a slash at the final part, you can ask for whatever comes after the last slash:
.*\/([^\/]+)
Your desired result will be at the group 1.
If the last part will always have the format name("string") then you can match this format as well - which strikes me as more explicit pattern:
\w+\(".*"\)

First occurrence of a character in string. Regular expression needed

I have some CSV data like this:
1325318514,197.1,184.9,172.4,146.0,147.3,131.1,280.9,182.7,12.6,5.0,0.0,73001,65848,0
1325318536,196.2,184.2,172.1,146.3,147.1,131.1,264.9,175.6,12.6,5.0,0.0,71590,64616,0
1325318557,196.6,184.9,172.1,147.6,146.8,130.9,264.9,178.4,12.6,5.0,0.0,69607,61274,0
1325318578,196.7,184.2,172.1,148.4,146.8,130.6,255.9,174.0,12.5,5.0,0.0,74127,59221,0
....
i want to replace the first , with a space on each string but not the rest of the ,s
Any ideas on the regexp for that? Tried a few different things and just cant seem to get it to work...
your question is not very clear, I just give an example, hope it is helpful for you:
sed 's/,/ /1' <<<"1325318514,197.1,184.9,172.4,146.0,147.3,131.1,280.9,182.7,12.6,5.0,0.0,73001,65848,0
1325318536,196.2,184.2,172.1,146.3,147.1,131.1,264.9,175.6,12.6,5.0,0.0,71590,64616,0
1325318557,196.6,184.9,172.1,147.6,146.8,130.9,264.9,178.4,12.6,5.0,0.0,69607,61274,0
1325318578,196.7,184.2,172.1,148.4,146.8,130.6,255.9,174.0,12.5,5.0,0.0,74127,59221,0"
output:
1325318514 197.1,184.9,172.4,146.0,147.3,131.1,280.9,182.7,12.6,5.0,0.0,73001,65848,0
1325318536 196.2,184.2,172.1,146.3,147.1,131.1,264.9,175.6,12.6,5.0,0.0,71590,64616,0
1325318557 196.6,184.9,172.1,147.6,146.8,130.9,264.9,178.4,12.6,5.0,0.0,69607,61274,0
1325318578 196.7,184.2,172.1,148.4,146.8,130.6,255.9,174.0,12.5,5.0,0.0,74127,59221,0
In most regex implementations, use the pattern
/([^,]*),(.*)/gm
And replacement text
$1 $2
See it online at http://refiddle.com/1ot

How do I extract the text before and after a known marker with a Perl regex?

Can anybody tell me how to identify the middle part interestedInThis and backreference the prefix: fontsize=12 and postfix: fontstyle=bold as ${1} and ${2}?
I'm dealing with this string:
<fontsize=12 interestedInThis fontstyle=bold>
Addendum: Sorry, I was not precise enough, here are the specifics:
prefix and postfix could be absent
prefix and postfix can be any string, not necessarily fontsize, resp. fontstyle
I know for sure, what I am looking for, namely interestedInThis and it will be separated through whitespaces.
<([^>]*)interestedInThis([^>]*)>
Basically
(<fontsize=12) (\S*) (fontstyle=bold>)
But, will the attribute values change? And, do you have to account for variable whitespace? If so, the above mutates into:
(<fontsize=\d+)\s+(\S*)\s+(fontstyle=.*>)
Also, in the above, by using \S, interestedInThis can contain anything that is not whitespace. If there is whitespace there too, for example interestedInThis is actually something like class="x" id="y", then maybe:
(<fontsize=\d+)(.*)(fontstyle=.*>)
Note that $2 is interestedInThis, and $1/$3 is actually your end pieces.
For your example, this could work
(<fontsize=\d+) (\w+) (fontstyle=bold>)
Unfortunatly, Perl doesn't seem to support named backreferences so I think you are stuck with <fontsize=12 in $1, ImInterestedInThis in $2 & fontstyle=bold> in $3.
regards,
Lieven
I think this is what you want;
<(.* )?InterestedInThis( .*)?>
It will return the the pre- and post-fix if they're there, but will still match if only one or neither are present.
It does have a minor problem that the spaces will be included in the tagged expressions, but that should be easy to remove after the match.
Alternatively, you could use lookahead / lookbehind to try to filter the spaces out as part of the match:
<(.*(?= ))? ?InterestedInThis ?((?<= ).*)?>
Try this:
my $result = m/(.*)(InterestedInThis)(.*)/;
Now:
$result is true if it found a match to the format.
InterestedInThis is in $2, though you already know what it is.
prefix (EVERYTHING before "InterestedInThis") is in $1.
postfix (EVERYTHING after "InterestedInThis") is in $3.