I need a regex so that it captures these:
/articles/123
/articles/123/something
/articles/123/something_else doesnt' matter
and doesn't these:
/articles
/articles/
/articles/123a
where 123 is integer
I tried this:
%r{^/articles/\d+} and %r{^/articles/\d+/*} but it also captured this /articles/1a
Use this:
^\/articles\/\d+(\/.*|$)
Conditionally match the end of the string using the $ character.
Here's a regex101 to play around with: https://regex101.com/r/dA1wD4/1
Related
Hello I have the following text:
some text,+
this field is another parameter
this is the final of the field
t10681374flp
t10681375flp
I would like to match the following two lines:
t10681374flp
t10681375flp
the rule is that these words begin with 't' and end with 'p',
I tried:
grep -e t*p testing
however I got:
this field is another parameter
t10681374flp
t10681375flp
So I really would like to appreciate support to overcome this task,
Using grep, to avoid matching strange lines and the perfect match, the code below
grep "^t[0-9]*flp$" testing
This matches the below lines,
t10681374flp
t10681375flp
This doesn't match the lines as below,
this field is another parameter
these dont grep
Hope you get resolved..
Following should do the work:
grep ^t.*p$ testing
^ indicates begining of the line, .* indicates any character
and $ indicates end of line.
I'm writing ruby and need some help with regex. And I'm really noob in regexp.
I have a string like this
/hello/world
I would like to #gsub this string to change the second slash to %2F.
The challange for me to ignore the first slash and to change only the second slash.
I tried this one
[^/]/
but it chooses not clean slash but o/ in
/hello/world
Please, help me. Thanks!!
You can simply capture the character before the slash in a group and use that in the replacement, for example:
"/hello/world".gsub(/([^\/])\//, '\1%2F') #=> "/hello%2Fworld"
Or if you just want to match any / that appears after the first character, you can simplify this to:
"/hello/world".gsub(/(.)\//, '\1%2F') #=> "/hello%2Fworld"
Or like this:
"/hello/world".gsub(/(?<!^)\//, '%2F') #=> "/hello%2Fworld"
And now for an uglier, regexless alternative:
"/hello/world".split("/").tap(&:shift).unshift("/").join("")
I'll see myself out.
You need to use subpattern within () for find substring:
/^\/(.*)$/
or
/^.(.*)$/
this pattern excluding first character. And then replace / in this substring
(?!^\/)\/
http://rubular.com/r/IRWptAJdLs is a a working example.
change the second / to %2F:
'/hello/world'.sub /(\/.*?)\//, '\1%2F'
#=> "/hello%2Fworld"
I was trying to work out a regular expression in IRB and got some unexpected output.
The goal was to match everything up until the last dot in a FQDN.
So, for example, if I was trying to match the string "flowtechconsulting.com",
I started with the following:
s1.sub(/^(.*)\\./, "\\1") #=> "flowtechconsultingcom"
However, the sub function simply returned everything but the dot, instead of the first matching group.
If I add two matching groups it works:
s1.sub(/^(.*)\\.(.*)$/, "\\1") #=> "flowtechconsulting"
I'm just not sure why the first doesn't work. It seems like it should.
/^(.*)\./ only captures everything up to the dot. The "com" is not captured and thus not replaced in the substitution.
Forget about sub, and do something like:
"foo.bar.baz.com"[/(.*)(?:\.)/, 1]
# => "foo.bar.baz"
I am building a project which users should be able to generate links easily by putting: #this is the link#. And i am trying to catch strings in between 2 # symbols with regex. I have tried,
#.+#
it works perfectly if only 1 link in users string, but if there are more than 1 links like,
#asdfasdf asdf# asdf asfasdfasdf asd fasd fasdf #asdfasdf asdfasdf asdf asdf#
it catches the whole string. But i need them separately, so i can substitute them with tags.
This is called "greedy regex". By default regular expression matches the longest string possible. You can make it non-greedy this way:
/#.+?#/
Demo: http://rubular.com/r/7WWyaUApFt
Use non-greedy match
#.+?#
It will catch indivisual ones.
I want to use regex in bash at line of variable assignment
e.g.
oldip="14\.130\.31\.172"
oldip_a="14.130.31.172" //How to use regex on this line.
How to use regex to del all '\' in $oldip? Then assign to new value to $oldip_a.
Do you have any idea?
I believe you want to use string replacement like this:
oldip_a=${oldip//\\/}
Or something like that... Of course there's always some battle escaping backslashes!
A more obvious example:
some_variable=${some_other_variable//replaceEachOfThese/withThis}
search for "replace all matches" on this page:
http://tldp.org/LDP/abs/html/string-manipulation.html
Here's how you can do it:
oldip="14\.130\.31\.172"
oldip_a=`echo $oldip | sed 's/[\]//g'`
echo $oldip_a
OUTPUT
14.130.31.172