简体   繁体   中英

Need regular expression for pattern this

I need a regular expression for below pattern

  1. It can start with / or number
  2. It can only contain numbers, no text
  3. Numbers can have space in between them.
  4. It can contain /* , at least 1 number and space or numbers and /*

Valid Strings:

 3232////33 43/323//
 3232////3343/323//
 /3232////343/323//

Invalid Strings:

/sas/3232/////dsds/    
/ /34343///// /////
///////////

My Problem is, it can have space between numbers like /3232 323/ but not / / .
How to validate it ?

I have tried so far:

(\\d[\\d ]*/+) , (/*\\d[\\d ]*/+) , (/*)(\\d*)(/*)

This regex should work for you:

^/*(?:\\d(?: \\d)*/*)+$

Live Demo: http://www.rubular.com/r/pUOYFwV8SQ

Try this java regex

/*(\\d[\\d ]*(?<=\\d)/+)+

It meets all your criteria.

Although you didn't specifically state it, I have assumed that a space may not appear as the first or last character for a number (ie spaces must be between numbers)

Just use lookarounds for the last criteria.

^(?=.*?\\d)([\\d/]*(?:/ ?(?!/)|\\d ?))+$

The best would have been to use conditional regex, but I think Java doesn't support them.

Explanation:
Basically, numbers or slashes, followed by one number and a space, or one slash and a space which is not followed by another slash. Repeat that. The space is made optional because I assume there's none at the end of your string.

我的解决方案不是那么简单,但是可以

^(((\d[\d ]*\d)|\d)|/)*((\d[\d ]*\d)|\d)(((\d[\d ]*\d)|\d)|/)*$
"(?![A-z])(?=.*[0-9].*)(?!.*/ /.*)[0-9/ ]{2,}(?![A-z])"

this will match what you want but keep in mind it will also match this

/3232///// from /sas/3232/////dsds/ 

this is because part of the invalid string is correct

if you reading line by line then match the ^ $ and if you are reading an entire block of text then search for \\r\\n around the regex above to match each new line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM