Regex for one string or a pattern - javascript

i am trying to write a regent for a line in this format:
Integer;characters etc;decimal number;decimal number;decimal
number;characters etc
For example:
989898;ABCD - eft;9809.98078;9878.876;8768.654;ABCD
For this, I have writen this which is working fine :
/[0-9]*;(.)*;[0-9\.]*;[0-9\.]*;[0-9\.]*;(.)/gi
Now, this problem is, there can be N.A. in the place of decimal number for which I am unable to get the refer right.

You can use alternation for this. Also, you can improve your regex a bit - for example [^;]* is more specific than .*, you can avoid repetition, and you don't need to escape the dot inside a character class:
/[0-9]*;[^;]*;(?:N\.A\.;|[0-9.]*;){3}.*/gi

Related

javascript regex - splitting a text into sentences on . if no number is before it

I am trying to parse a text into sentences, by using:
srt.replace(/(\.+|:|!|\?)(\s|\n|\r|\r\n)/gm, "$1$2|").split("|");
Which works great, but... If a sentence starts with a list number (i.e "1. some words") I get: ['1.', 'some words'].
It's my first time using regex and while I know there's a way to lookbehind I was not able to use it.
How can I change my regex to only split at . if there's no number character before it?
Ended up using str.replace(/(?<!:)(\n)\s*/g, "$1|").replace(/(?<![0-9])(\.+)\s*/g, "$1|").replace(/(\?+|!+)\s*/g, "$1|").split("|")
I am sure there's a prettier way to write this regex, but as a noob - I don't yet know how. This also covers:
1. Not splitting if there's a new line after :
2. Multiple dots, question and exclamation marks
This code is meant to split a text into "ideas", which is why I used the conditions I did, might not be the right logic for a simple "split to sentences" need.

javascript regexp to match path depth

Been struggling for the last hour to try and get this regexp to work but cannot seem to crack it.
It must be a regexp and I cannot use split etc as it is part of a bigger regexp that searches for numerous other strings using .test().
(public\/css.*[!\/]?)
public/css/somefile.css
public/css/somepath/somefile.css
public/css/somepath/anotherpath/somefile.css
Here I am trying to look for path starting with public/css followed by any character except for another forward slash.
so "public/css/somefile.css" should match but the other 2 should not.
A better solution may be to somehow specify the number of levels to match after the prefix using something like
(public\/css\/{1,2}.*)
but I can't seem to figure that out either, some help with this would be appreciated.
edit
No idea why this question has been marked down twice, I have clearly stated the requirement with sample code and test cases and also attempted to solve the issue, why is it being marked down ?
You can use this regex:
/^(public\/css\/[^\/]*?)$/gm
^ : Starts with
[^/] : Not /
*?: Any Characters
$: Ends with
g: Global Flag
m: Multi-line Flag
Something like this?
/public\/css\/[^\/]+$/
This will match
public/css/[Any characters except for /]$
$ is matching the end of the string in regex.

Capturing optional part of URL with RegExp

While writing an API service for my site, I realized that String.split() won't do it much longer, and decided to try my luck with regular expressions. I have almost done it but I can't find the last bit. Here is what I want to do:
The URL represents a function call:
/api/SECTION/FUNCTION/[PARAMS]
This last part, including the slash, is optional. Some functions display a JSON reply without having to receive any arguments. Example: /api/sounds/getAllSoundpacks prints a list of available sound packs. Though, /api/sounds/getPack/8Bit prints the detailed information.
Here is the expression I have tried:
req.url.match(/\/(.*)\/(.*)\/?(.*)/);
What am I missing to make the last part optional - or capture it in whole?
This will capture everything after FUNCTION/ in your URL, independent of the appearance of any further / after FUNCTION/:
FUNCTION\/(.+)$
The RegExp will not match if there is no part after FUNCTION.
This regex should work by making last slash and part after optional:
/^\/[^/]*\/[^/]*(?:\/.*)?$/
This matches all of these strings:
/api/SECTION/FUNCTION/abc
/api/SECTION
/api/SECTION/
/api/SECTION/FUNCTION
Your pattern /(.*)/(.*)/?(.*) was almost correct, it's just a bit too short - it allows 2 or 3 slashes, but you want to accept anything with 3 or 4 slashes. And if you want to capture the last (optional) slash AND any text behind it as a whole, you simply need to create a group around that section and make it optional:
/.*/.*/.*(?:/.+)?
should do the trick.
Demo. (The pattern looks different because multiline mode is enabled, but it still works. It's also a little "better" because it won't match garbage like "///".)

Regex for Matching numeral Pinyin

I'm looking for a regex to match numeral pinyin lexical unit (one or more pinyin without space).
Reading Regex for Matching Pinyin seems a good start as I was able to quickly add the support for numeral by doing :
/(ORIGINAL_REGEXP)[0-5]/
So essentially wrapping the old regexp in a group and appending the numeral condition.
However I'm not able to extend this to the case of multiple words. For instance :
jiao4zuo4zhi1wu4 叫座之物
jiao4zu3 教祖
jiao4zong1xuan3ju3 教宗选举
jiao4zi3 教子
jiao4zhun3yi2qi4 校准仪器
jiao4zhun3tiao2 校准条
jiao4zhun3ti1chi3 校准梯尺
jiao4zhun3quan1 校准圈
jiao4zhun3qi4 校准器
jiao4zhun3pu3 校准谱
N.B.: This expression will be used in a Javascript context.
I might be interpreting your question the wrong way, but couldn't you just add a + for one or more pinyins? I.e.
/((ORIGINAL_REGEXP)[0-5])+/
Here is the regexp I'm using based on #EagleV_Attnam solution and some addition what I've done fin:
/^((ORIGINAL_REGEXP)[0-5])+$/
The addition of the start ^ and end $ anchor solve my issues :)
Full regex is:
/^((([mM]iu|[pmPM]ou|[bpmBPM](o|e(i|ng?)?|a(ng?|i|o)?|i(e|ng?|a[no])?|u))|([fF](ou?|[ae](ng?|i)?|u))|([dD](e(i|ng?)|i(a[on]?|u))|[dtDT](a(i|ng?|o)?|e(i|ng)?|i(a[on]?|e|ng|u)?|o(ng?|u)|u(o|i|an?|n)?))|([nN]eng?|[lnLN](a(i|ng?|o)?|e(i|ng)?|i(ang|a[on]?|e|ng?|u)?|o(ng?|u)|u(o|i|an?|n)?|ve?))|([ghkGHK](a(i|ng?|o)?|e(i|ng?)?|o(u|ng)|u(a(i|ng?)?|i|n|o)?))|([zZ]h?ei|[czCZ]h?(e(ng?)?|o(ng?|u)?|ao|u?a(i|ng?)?|u?(o|i|n)?))|([sS]ong|[sS]hua(i|ng?)?|[sS]hei|[sS][h]?(a(i|ng?|o)?|en?g?|ou|u(a?n|o|i)?|i))|([rR]([ae]ng?|i|e|ao|ou|ong|u[oin]|ua?n?))|([jqxJQX](i(a(o|ng?)?|[eu]|ong|ng?)?|u(e|a?n)?))|(([aA](i|o|ng?)?|[oO]u?|[eE](i|ng?|r)?))|([wW](a(i|ng?)?|o|e(i|ng?)?|u))|[yY](a(o|ng?)?|e|in?g?|o(u|ng)?|u(e|a?n)?))[0-5])+$/

Regular expression for "-" in javascript

I want to know how can I get a regular expression for matching number, ., and - only.
I am using this:
/^[0-9\.'S]+$/
by this it working fine but not working for symbol "-".
You simply haven't used the literal dash - (or minus) in the regex. Try:
/^[0-9\.-]+$/
But if you want a proper number, you might want to use a more proper regex:
/^-?[0-9]+(?:\.[0-9]+)?$/
The first regex can accept things such as 3987----.... while the second will not accept it, but will accept things like -87.983274.
That's because - is not part of your character class. You are only using - in the class range (which only includes digits). Also, I don't know what the S and the ' are doing there:
/^[0-9.-]+$/
Also, I can promise you that after taking the time to read through this tutorial regular expressions will seem a lot less confusing to you.
Try the below regex.
/^-?[0-9\.]+$/

Categories