Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
Regex: select until first space or comma occurrence

I have following example of american addresses.

6301 Stonewood Dr Apt-728, Plano TX-75024 
13323 Maham Road, Apt # 1621, Dallas, TX 75240
17040 Carlson Drive, #1027 Parker, CO 80134
3465 25th St., San Francisco, CA 94110 

I want to extract city from using regex

Plano, Dallas, Parker, San Francisco

I am using following regex which is working for first example

(?<=[,.|•]).*\s+(?=[\s,.]?CA?[\s,.-]?[\d]{4,})

can you help me for the same as?

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

You can use

,(?:\s*#\d+)?\s*([^\s,][^,]*)(?=\W+[A-Z]{2}\W*\d{4,}\s*$)

See the regex demo. The necessary value is in Group 1.

Details:

  • , - a comma
  • (?:\s*#\d+)? - an optional sequence of zero or more whitespaces, # and then one or more digits
  • \s* - zero or more whitespaces
  • ([^\s,][^,]*) - Group 1: a char other than whitespace and comma and then zero or more non-comma chars
  • (?=\W+[A-Z]{2}\W*\d{4,}\s*$) - a positive lookahead that requires (immediately on the right)
    • \W+ - one or more non-word chars
    • [A-Z]{2} - two uppercase ASCII letters
    • \W* - zero or more non-word chars
    • \d{4,} - gfour or more digits
    • \s* - zero or more whitespaces
    • $ - end of string.
about 4 years ago · Santiago Gelvez Relatório

0

You can match the comma, then all except A-Z and capture from the first occurrence of A-Z.

,[^A-Z,]*?\b([A-Z][^,]*?),?\s*[A-Z]{2}[-\s]\d{4,}\s*$

Explanation

  • ,[^A-Z,]*?\b Match a comma, then any char except A-Z or a comma till a word boundary
  • ([A-Z][^,]*?) Capture group 1 Match A-Z and then any char except a comma as least as possible
  • ,?\s*[A-Z]{2} match optional comma, optional whiteapace chars and 2 uppecase chars A-Z
  • [-\s]\d{4,}\s* Match either - or a whitespace char and then 4 or more digits followed by optional whiteapace chars
  • $ end of string

Regex demo

about 4 years ago · Santiago Gelvez Relatório

0

The best guess I could achieve was starting from the end of the string looking for a chain of non-spacing characters (being the portion you are looking for) followed by a space, a chain of capital letters, then an option space/dash and in the end a chain of numbers.

([^\s]+?)\,?\s[A-Z]+[\s\-]?\d+$

Being the first group, the target you are aiming for.

This is a live example with your use case embedded:

https://regexr.com/6nkq5

(as a side note, the demo on regexr may tell you the expression took more than 250ms and can't render.. you just slightly edit the test case to make it update and show you the actual result)

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda